When you create an Object FC Access Point, OSS automatically generates an alias for it. Use the alias in place of the bucket name to route GetObject requests through Function Compute for processing before the response is returned.
Prerequisites
Before you begin, make sure that you have:
A function compiled to process GetObject requests. For details, see Compile a function that is used to process GetObject requests.
The alias of your Object FC Access Point. The alias is generated automatically when you create the access point, and follows the format
{access-point-name}-{account-hash}-opapalias. To find your alias, go to the Object FC Access Points page in the OSS console and copy the Alias value for your access point.
Use OSS SDKs
All examples below pass the Object FC Access Point alias as the bucket name. This routes GetObject requests through Function Compute automatically.
Java
Requires OSS SDK for Java 3.17.2 or later.
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import java.io.File;
public class Demo {
public static void main(String[] args) throws Exception {
// Public endpoint. To use the internal network, replace with the internal endpoint,
// for example: https://oss-cn-qingdao-internal.aliyuncs.com
String endpoint = "https://oss-cn-qingdao.aliyuncs.com";
// Load access credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Replace with your Object FC Access Point alias.
String bucketName = "fc-ap-01-3b00521f653d2b3223680ec39dbbe2****-opapalias";
// Full object path, excluding the bucket name.
String objectName = "exampledir/exampleobject.txt";
// Local path to save the downloaded file.
String pathName = "/tmp/exampleobject.txt";
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
try {
// Download the object to a local file. If a file with the same name exists, it is overwritten.
ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(pathName));
} catch (OSSException oe) {
System.out.println("OSS error — request reached OSS but was rejected.");
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("Client error — could not reach OSS.");
System.out.println("Error Message: " + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}Python
Requires OSS SDK for Python 2.18.3 or later.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Load access credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Public endpoint. To use the internal network, replace with the internal endpoint,
# for example: https://oss-cn-qingdao-internal.aliyuncs.com
endpoint = "https://oss-cn-qingdao.aliyuncs.com"
# Replace with your Object FC Access Point alias.
bucket_name = "fc-ap-01-3b00521f653d2b3223680ec39dbbe2****-opapalias"
bucket = oss2.Bucket(auth, endpoint=endpoint, bucket_name=bucket_name)
# object_name: full object path, excluding the bucket name.
# local_file: local path to save the downloaded file. Existing files are overwritten.
bucket.get_object_to_file('exampledir/exampleobject.txt', '/tmp/exampleobject.txt')Go
Requires OSS SDK for Go 1.2.2 or later.
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"os"
)
func main() {
// Load access credentials from environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the endpoint for the region where your bucket is located.
client, err := oss.New("http://oss-cn-qingdao.aliyuncs.com", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Replace with your Object FC Access Point alias.
bucketName := "fc-ap-01-3b00521f653d2b3223680ec39dbbe2****-opapalias"
bucket, err := client.Bucket(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// object_name: full object path, excluding the bucket name.
// local_file: local path to save the downloaded file.
err = bucket.GetObjectToFile("exampledir/exampleobject.txt", "/tmp/exampleobject.txt")
if err != nil {
fmt.Println("GetObject error:", err)
os.Exit(-1)
}
fmt.Println("success")
}Use ossutil
Set the bucket name in the OSS URL to the Object FC Access Point alias.
ossutil cp oss://fc-ap-01-3b00521f653d2b3223680ec39dbbe2****-opapalias/demo.txt /Users/demo/Desktop/demo.txtFor a full list of ossutil commands, see Common commands.