Após a criação de um Object FC Access Point, o Object Storage Service (OSS) gera automaticamente um alias para ele. Use esse alias para chamar a operação GetObject.
Pré-requisitos
Você deve ter escrito uma função para processar requisições GetObject. Para mais informações, consulte Escrever uma função de requisição.
Usar SDKs
Java
O OSS SDK for Java 3.17.2 e versões posteriores permite acessar recursos do OSS usando o alias de um Object FC Access Point.
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 {
// Use the public endpoint of the Object FC Access Point to access OSS.
String endpoint = "https://oss-cn-qingdao.aliyuncs.com";
// Specify the region that corresponds to the endpoint. For example, cn-qingdao.
String region = "cn-qingdao";
// Use the internal endpoint of the Object FC Access Point to access OSS.
// String endpoint = "https://oss-cn-qingdao-internal.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the alias of the Object FC Access Point.
String bucketName = "fc-ap-01-3b00521f653d2b3223680ec39dbbe2****-opapalias";
// Specify the full path of the object. Do not include the bucket name in the path.
String objectName = "yourObjectName";
// Specify the full path to which the object is downloaded.
String pathName = "yourPathName";
// Create an OSSClient instance.
// When the OSSClient instance is no longer used, call the shutdown method to release resources.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
// Explicitly declare the use of the V4 signature algorithm.
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Download the object to a local file and save it to the specified path. If the local file exists, it is overwritten. If the local file does not exist, a new file is created.
// If a local path is not specified, the downloaded file is saved to the path of the project that contains the sample program.
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
O OSS SDK for Python 2.18.3 e versões posteriores permite acessar recursos do OSS usando o alias de um Object FC Access Point.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Use the public endpoint of the Object FC Access Point to access OSS.
endpoint = "https://oss-cn-qingdao.aliyuncs.com"
# Use the internal endpoint of the Object FC Access Point to access OSS.
# endpoint = "https://oss-cn-qingdao-internal.aliyuncs.com"
# Specify the alias of the Object FC Access Point.
bucket_name = "fc-ap-01-3b00521f653d2b3223680ec39dbbe2****-opapalias"
bucket = oss2.Bucket(auth, endpoint=endpoint, bucket_name=bucket_name)
# Set yourObjectName to the full path of the object. Do not include the bucket name in the path.
# Set yourLocalFile to the path of the local file. If the local file exists, it is overwritten. If the local file does not exist, a new file is created.
bucket.get_object_to_file('yourObjectName', 'yourLocalFile')
Go
O OSS SDK for Go 1.2.2 e versões posteriores permite acessar recursos do OSS usando o alias de um Object FC Access Point.
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)
}
// Create an OSSClient instance.
// Set yourEndpoint to the endpoint of the bucket. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, specify the actual endpoint.
client, err := oss.New("http://oss-cn-qingdao.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the alias of the Object FC Access Point.
bucketName := "fc-ap-01-3b00521f653d2b3223680ec39dbbe2****-opapalias"
bucket, err := client.Bucket(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Set yourObjectName to the full path of the object. Do not include the bucket name in the path.
// Set yourLocalFile to the full path of the local file.
err = bucket.GetObjectToFile("yourObjectName", "yourLocalFile")
if err != nil {
fmt.Println("GetObject Error:", err)
os.Exit(-1)
}
fmt.Println("success")
}