创建对象FC接入点后,OSS会自动为您生成对象FC接入点别名。您可以使用对象FC接入点别名访问GetObject接口。
前提条件
已编写处理GetObject请求的函数。具体操作,请参见编写处理GetObject请求的函数。
使用SDK
Java
Java SDK 3.17.2及以上版本支持通过对象FC接入点别名的方式访问OSS资源。
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.File;
public class Demo {
public static void main(String[] args) throws Exception {
// 使用对象FC接入点的外网Endpoint进行访问。
String endpoint = "https://oss-cn-qingdao.aliyuncs.com";
// 填写Endpoint对应的Region信息,例如cn-hangzhou。
String region = "cn-hangzhou";
// 使用对象FC接入点的内网Endpoint进行访问。
// String endpoint = "https://oss-cn-qingdao-internal.aliyuncs.com";
// 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// 填写对象FC接入点别名。
String bucketName = "fc-ap-01-3b00521f653d2b3223680ec39dbbe2****-opapalias";
// 填写不包含Bucket名称在内的Object完整路径。
String objectName = "yourObjectName";
// 填写下载到本地的完整路径。
String pathName = "yourPathName";
// 创建OSSClient实例。
// 当OSSClient实例不再使用时,调用shutdown方法以释放资源。
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
// 显式声明使用 V4 签名算法
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// 下载Object到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。
// 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。
ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(pathName));
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
Python
Python SDK 2.18.3及以上版本支持通过对象FC接入点别名的方式访问OSS资源。
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# 使用对象FC接入点的外网Endpoint进行访问。
endpoint = "https://oss-cn-qingdao.aliyuncs.com"
# 使用对象FC接入点的内网Endpoint进行访问。。
# endpoint = "https://oss-cn-qingdao-internal.aliyuncs.com"
# 填写对象FC接入点别名。
bucket_name = "fc-ap-01-3b00521f653d2b3223680ec39dbbe2****-opapalias"
bucket = oss2.Bucket(auth, endpoint=endpoint, bucket_name=bucket_name)
# yourObjectName填写Object完整路径,完整路径中不包含Bucket名称。
# yourLocalFile填写本地文件路径。如果指定的本地文件存在会覆盖,不存在则新建。
bucket.get_object_to_file('yourObjectName', 'yourLocalFile')
Go
Go SDK 1.2.2及以上版本支持通过对象FC接入点别名的方式访问OSS资源。
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func main() {
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// 创建OSSClient实例。
// yourEndpoint填写Bucket对应的Endpoint,以华东1(杭州)为例,填写为https://oss-cn-hangzhou.aliyuncs.com。其它Region请按实际情况填写。
client, err := oss.New("http://oss-cn-qingdao.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// 填写对象FC接入点别名。
bucketName := "fc-ap-01-3b00521f653d2b3223680ec39dbbe2****-opapalias"
bucket, err := client.Bucket(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// yourObjectName填写不包含Bucket名称在内的Object完整路径。
// yourLocalFile填写下载到本地的完整路径。
err = bucket.GetObjectToFile("yourObjectName", "yourLocalFile")
if err != nil {
fmt.Println("GetObject Error:", err)
os.Exit(-1)
}
fmt.Println("success")
}