How to use an alias for your Object FC Access Point

Updated at:

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

Prerequisites

You have written a function to process GetObject requests. For more information, see Write a request function.

Use SDKs

Java

OSS SDK for Java 3.17.2 and later lets you access OSS resources using the alias of an 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

OSS SDK for Python 2.18.3 and later lets you access OSS resources 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.
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

OSS SDK for Go 1.2.2 and later lets you access OSS resources 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.
  // 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")
}

Use the ossutil command line interface

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

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

For more command examples, see Common commands.

Use a REST API

When you call the GetObject operation to request OSS resources, include the alias of the Object FC Access Point in the Host header. For example:

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