Bu konu, yükleme veya indirme bant genişliği sınırını belirlemek için nesne yükleme ya da indirme isteğine parametrelerin nasıl ekleneceğini açıklar. Bu sayede diğer uygulamalar için yeterli bant genişliği korunur.
Notlar
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek istiyorsanız dahili uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. Erişim kimlik bilgilerini yapılandırma.
Bu konuda bir OSS uç noktası kullanarak OSSClient örneği oluşturma işlemi gösterilmektedir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. İstemci yapılandırma (Go SDK V1).
Örnek kod
Aşağıdaki örnek kod, basit yükleme ve indirme işlemleri için tek bağlantı bant genişliği kısıtlamasının nasıl yapılandırılacağını gösterir:
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")
}
Yaygın senaryolar
Çok parçalı yükleme için tek bağlantı bant genişliği kısıtlaması yapılandırma
Ekleme yoluyla yükleme için bant genişliği kısıtlaması yapılandırma
İmzalı URL'ler kullanan yükleme ve indirme işlemleri için bant genişliği kısıtlaması yapılandırma
İlgili Konular
Bir nesne yüklemek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. PutObject.
Nesneleri indirmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetObjectToFile.