すべてのプロダクト
Search
ドキュメントセンター

Object Storage Service:シングル接続帯域幅スロットリングの設定

最終更新日:Dec 05, 2024

このトピックでは、オブジェクトのアップロードまたはダウンロード要求にパラメーターを追加して、アップロードまたはダウンロード帯域幅の制限を設定する方法について説明します。 これにより、他のアプリケーションに十分な帯域幅が確保されます。

使用上の注意

  • このトピックでは、中国 (杭州) リージョンのパブリックエンドポイントを使用します。 OSSと同じリージョンにある他のAlibaba CloudサービスからOSSにアクセスする場合は、内部エンドポイントを使用します。 OSSリージョンとエンドポイントの詳細については、「リージョン、エンドポイント、オープンポート」をご参照ください。

  • このトピックでは、アクセス資格情報は環境変数から取得します。 アクセス資格情報の設定方法の詳細については、「アクセス資格情報の設定」をご参照ください。

  • このトピックでは、OSSエンドポイントを使用してOSSClientインスタンスを作成します。 カスタムドメイン名またはSTS (Security Token Service) を使用してOSSClientインスタンスを作成する場合は、「初期化」をご参照ください。

サンプルコード

次のサンプルコードは、単純なアップロードとダウンロードのために単一接続帯域幅スロットリングを構成する方法の例を示しています。

package main

import (
	"log"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// 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. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	options := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	options = append(options, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	options = append(options, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", options...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Specify the full path of the local file that you want to upload. Example: D:\\localpath\\examplefile.txt. 
	// If you do not specify the path of the local file, the local file is uploaded from the path of the project to which the sample program belongs. 
	localFilePath := "D:\\localpath\\examplefile.txt"
	fd, err := os.Open(localFilePath)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFilePath, err)
	}
	defer fd.Close()

	// Specify the bandwidth limit for the upload. The value of the parameter must be a number. Default unit: bit/s. In this example, the bandwidth limit is set to 5 MB/s. 
	var traffic int64 = 41943040

	// Configure bandwidth throttling for object upload. 
	// Specify the full path of the object. Do not include the bucket name in the full path. 
	objectName := "exampledir/exampleobject.txt"
	err = bucket.PutObject(objectName, fd, oss.TrafficLimitHeader(traffic))
	if err != nil {
		log.Fatalf("Failed to upload object '%s': %v", objectName, err)
	}

	// Configure bandwidth throttling for object download. 
	// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. Then, specify the full path of the local file. Example: D:\\localpath\\exampleobject.txt. 
	downloadFilePath := "D:\\localpath\\exampleobject.txt"
	err = bucket.GetObjectToFile(objectName, downloadFilePath, oss.TrafficLimitHeader(traffic))
	if err != nil {
		log.Fatalf("Failed to download object '%s' to '%s': %v", objectName, downloadFilePath, err)
	}

	log.Println("Upload and download completed successfully")
}

一般的なシナリオ

マルチパートアップロードのシングル接続帯域幅スロットリングの設定

次のコードは、マルチパートアップロードのシングル接続帯域幅スロットリングを設定する方法の例を示しています。

package main

import (
	"io"
	"log"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// 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. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	options := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	options = append(options, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	options = append(options, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", options...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Specify the bandwidth limit for the upload. The value of the parameter must be a number. Default unit: bit/s. In this example, the bandwidth limit is set to 5 MB/s. 
	traffic := int64(41943040)

	// Upload a large object by using multipart upload. 
	// You can split an object into multiple parts based on the object size. In this example, the object is split into three parts. 
	localFilePath := "localFile"
	chunks, err := oss.SplitFileByPartNum(localFilePath, 3)
	if err != nil {
		log.Fatalf("Failed to split file '%s': %v", localFilePath, err)
	}

	// Open the object. 
	fd, err := os.Open(localFilePath)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFilePath, err)
	}
	defer fd.Close()

	// Initiate a multipart upload task for the object. 
	objectName := "exampledir/exampleobject.txt"
	imur, err := bucket.InitiateMultipartUpload(objectName)
	if err != nil {
		log.Fatalf("Failed to initiate multipart upload for '%s': %v", objectName, err)
	}

	// Upload parts and configure bandwidth throttling. 
	var parts []oss.UploadPart
	for _, chunk := range chunks {
		_, err := fd.Seek(chunk.Offset, io.SeekStart)
		if err != nil {
			log.Fatalf("Failed to seek to offset %d in file '%s': %v", chunk.Offset, localFilePath, err)
		}
		part, err := bucket.UploadPart(imur, fd, chunk.Size, chunk.Number, oss.TrafficLimitHeader(traffic))
		if err != nil {
			log.Fatalf("Failed to upload part %d of '%s': %v", chunk.Number, objectName, err)
		}
		parts = append(parts, part)
	}

	// Complete the upload. 
	_, err = bucket.CompleteMultipartUpload(imur, parts)
	if err != nil {
		log.Fatalf("Failed to complete multipart upload for '%s': %v", objectName, err)
	}

	log.Println("Multipart upload completed successfully")
}

追加アップロードの帯域幅調整の設定

次のコードでは、追加アップロードのシングル接続帯域幅スロットリングを設定する方法の例を示します。

package main

import (
	"log"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// 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. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	options := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	options = append(options, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	options = append(options, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", options...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Specify the bandwidth limit for the upload. The value of the parameter must be a number. Default unit: bit/s. In this example, the bandwidth limit is set to 5 MB/s. 
	traffic := int64(41943040)

	// Perform the append upload. 
	// localFileOne and localFileTwo indicates the full paths of the two local files to upload. After localFileOne is uploaded, localFileTwo is uploaded by using append upload. 
	localFileOne := "localFileOne"
	localFileTwo := "localFileTwo"

	// Open localFileOne.
	fd1, err := os.Open(localFileOne)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFileOne, err)
	}
	defer fd1.Close()

	// Open localFileTwo.
	fd2, err := os.Open(localFileTwo)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFileTwo, err)
	}
	defer fd2.Close()

	// Specify the full path of the object.
	objectName := "exampledir/exampleobject.txt"

	// Initiate nextPos.
	var nextPos int64

	// Append localFileOne.
	nextPos, err = bucket.AppendObject(objectName, fd1, nextPos, oss.TrafficLimitHeader(traffic))
	if err != nil {
		log.Fatalf("Failed to append object '%s' from file '%s': %v", objectName, localFileOne, err)
	}

	// Append localFileTwo.
	nextPos, err = bucket.AppendObject(objectName, fd2, nextPos, oss.TrafficLimitHeader(traffic))
	if err != nil {
		log.Fatalf("Failed to append object '%s' from file '%s': %v", objectName, localFileTwo, err)
	}

	log.Println("Append upload completed successfully")
}

署名付きURLを使用するアップロードとダウンロードの帯域幅調整を構成する

次のサンプルコードでは、署名付きURLを使用してオブジェクトをアップロードまたはダウンロードする場合に、単一接続帯域幅スロットリングを構成する方法の例を示します。

package main

import (
	"log"
	"os"

	"github.com/aliyun/aliyun-oss-go-sdk/oss"
)

func main() {
	// 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. 
	provider, err := oss.NewEnvironmentVariableCredentialsProvider()
	if err != nil {
		log.Fatalf("Failed to create credentials provider: %v", err)
	}

	// 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. 
	// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
	clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
	clientOptions = append(clientOptions, oss.Region("yourRegion"))
	// Specify the version of the signature algorithm.
	clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
	client, err := oss.New("yourEndpoint", "", "", clientOptions...)
	if err != nil {
		log.Fatalf("Failed to create OSS client: %v", err)
	}

	// Specify the name of the bucket. Example: examplebucket. 
	bucketName := "examplebucket"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Failed to get bucket '%s': %v", bucketName, err)
	}

	// Use the signed URL to upload the object. 
	// Specify the full path of the local file to upload. Example: D:\\localpath\\exampleobject.txt. 
	localFilePath := "D:\\localpath\\exampleobject.txt"
	fd, err := os.Open(localFilePath)
	if err != nil {
		log.Fatalf("Failed to open local file '%s': %v", localFilePath, err)
	}
	defer fd.Close()

	// Specify the bandwidth limit for the upload. The value of the parameter must be a number. Default unit: bit/s. In this example, the bandwidth limit is set to 5 MB/s. 
	traffic := int64(41943040)

	// Obtain the signed URL used to upload the object. 
	// Specify the full path of the object. Do not include the bucket name in the full path. 
	objectName := "exampledir/exampleobject.txt"
	strURL, err := bucket.SignURL(objectName, oss.HTTPPut, 60, oss.TrafficLimitParam(traffic))
	if err != nil {
		log.Fatalf("Failed to generate signed URL for uploading '%s': %v", objectName, err)
	}

	// Upload the local file. 
	err = bucket.PutObjectWithURL(strURL, fd)
	if err != nil {
		log.Fatalf("Failed to upload object '%s': %v", objectName, err)
	}

	// Use the signed URL to download the object. 
	// Obtain the signed URL that is used to download the object. 
	strURL, err = bucket.SignURL(objectName, oss.HTTPGet, 60, oss.TrafficLimitParam(traffic))
	if err != nil {
		log.Fatalf("Failed to generate signed URL for downloading '%s': %v", objectName, err)
	}

	// Specify the full path to which you want to download the object. 
	downloadFilePath := "D:\\localpath\\exampleobject.txt"
	err = bucket.GetObjectToFileWithURL(strURL, downloadFilePath)
	if err != nil {
		log.Fatalf("Failed to download object '%s' to '%s': %v", objectName, downloadFilePath, err)
	}

	log.Println("Upload and download completed successfully")
}

関連ドキュメント

  • オブジェクトをアップロードするために呼び出すことができるAPI操作の詳細については、「PutObject」をご参照ください。

  • オブジェクトをダウンロードするために呼び出すAPI操作の詳細については、「GetObjectToFile」をご参照ください。