All Products
Search
Document Center

Object Storage Service:Use Object FC Access Points

Last Updated:Jun 14, 2024

After you create an Object FC Access Point, Object Storage Service (OSS) automatically generates an alias for the Object FC Access Point. You can use the alias to call the GetObject operation.

Prerequisites

A function that is used to process GetObject requests is compiled. For more information, see Compile a function that is used to process GetObject requests.

Use OSS SDKs

Java

OSS SDK for Java 3.17.2 and later allow you to access OSS resources by using the alias of an Object FC Access Point.

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 {
        // Use the public endpoint of the Object FC Access Point to access OSS resources. 
        String endpoint = "https://oss-cn-qingdao.aliyuncs.com";
        // Use the internal endpoint of the Object FC Access Point to access OSS resources. 
        // String endpoint = "https://oss-cn-qingdao-internal.aliyuncs.com";
        // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
        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 full path. 
        String objectName = "yourObjectName";
        // Specify the full path of the downloaded object on your computer. 
        String pathName = "yourPathName";

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            // Download the object as a local file in the specified path. If a file that has the same name already exists in the path, the downloaded object overwrites the file. If no file that has the same name exists in the path, the downloaded object is saved in the path. 
            // If you do not specify a local path for the downloaded object, the downloaded object is saved to the path of the project to which the sample program belongs. 
            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

OSS SDK for Python 2.18.3 and later allow you to access OSS resources by using the alias of an 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 resources. 
endpoint = "https://oss-cn-qingdao.aliyuncs.com"
# Use the internal endpoint of the Object FC Access Point to access OSS resources.  
# 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 full path. 
# Set yourLocalFile to the full path of the downloaded object on your computer. If a file that has the same name already exists in the path, the downloaded object overwrites the file. If no file that has the same name exists in the path, the downloaded object is saved in the path. 
bucket.get_object_to_file('yourObjectName', 'yourLocalFile')

Go

OSS SDK for Go 1.2.2 and later allow you to access OSS resources by using the alias of an 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. 
  // Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your 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 full path. 
  // Set yourLocalFile to the full path of the downloaded object on your computer. 
  err = bucket.GetObjectToFile("yourObjectName", "yourLocalFile")
  if err != nil {
    fmt.Println("GetObject Error:", err)
    os.Exit(-1)
  }
  fmt.Println("success")
}

Use ossutil

When you use ossutil to request OSS resources, you must set the bucket name to the alias of an Object FC Access Point. Example:

ossutil cp oss://fc-ap-01-3b00521f653d2b3223680ec39dbbe2****-opapalias/demo.txt /Users/demo/Desktop/demo.txt

For more information about common commands, see Common commands.

Use the OSS API

When you call the GetObject operation to request OSS resources, you must add the alias of an Object FC Access Point to the Host header. Example:

GET /ObjectName HTTP/1.1
Host: fc-ap-01-3b00521f653d2b3223680ec39dbbe2****-opapalias.oss-cn-qingdao.aliyuncs.com
Date: GMT Date
Authorization: SignatureValue