Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Range download (OSS SDK for Go 1.0)

Son Güncelleme:Oct 01, 2026

Bu konuda, bir nesneden belirtilen veri aralığının verimli bir şekilde nasıl indirileceği açıklanmaktadır.

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 bir OSSClient örneğinin nasıl oluşturulacağı gösterilmektedir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. İstemciyi yapılandırma (Go SDK V1).

  • Aralık indirerek indirme işlemi yapabilmek için oss:GetObject iznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel bir ilke atama.

Örnek kod

Aşağıdaki örnek kod, bir nesnenin belirli bir veri aralığını indirmek için kullanılır.

package main

import (
	"io"
	"log"
	"strings"

	"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("Error: %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("Error: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	
	// Upload 1,000 bytes.
	strContent := ""
	for i := 0; i < 100; i++ {
		strContent += "abcdefghij"
	}
	log.Printf("content len: %d\n", len(strContent))

	// Upload a string.
	objectName := "yourObjectName"
	err = bucket.PutObject(objectName, strings.NewReader(strContent))
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	// Query the data that is within the range from byte 15 to byte 35, which includes a total of 21 bytes. 
	// If the specified value range is invalid, the entire object is downloaded. For example, if the specified range includes a negative number or the specified value is greater than the object size, all content of the object is downloaded.
	body, err := bucket.GetObject(objectName, oss.Range(15, 35))
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	defer body.Close()

	// Read the data and load it into the memory.
	data, err := io.ReadAll(body)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	log.Printf("data: %s", string(data))
}

SSS

Geçersiz bir veri indirme aralığı belirtilirse ne olur?

Boyutu 1.000 bayt olan bir nesne için geçerli aralık 0. bayttan 999. bayta kadardır. Belirtilen aralık 0. bayt ile 999. bayt arasında değilse aralık geçerli olmaz. Bu durumda OSS HTTP durum kodu 200 döndürür ve nesnenin tüm verilerini sunar. Aşağıdaki örneklerde geçersiz istekler ve döndürülen sonuçlar gösterilmektedir:

  • Range: bytes değerini 500-2000 olarak ayarlarsanız aralığın sonundaki değer geçersiz olur. Bu durumda OSS HTTP durum kodu 200 döndürür ve nesnenin tüm verilerini sunar.

  • Range: bytes değerini 1000-2000 olarak ayarlarsanız aralığın başındaki değer geçersiz olur. Bu durumda OSS HTTP durum kodu 200 döndürür ve nesnenin tüm verilerini sunar.

Aralığa göre veri indirmek için standart davranışlar nasıl belirtilir?

İstek üstbilgisine x-oss-range-behavior:standard eklerseniz belirtilen aralık geçerli aralık dışında olduğunda indirme davranışı değiştirilir. Boyutu 1.000 bayt olan bir nesne için:

  • Range: bytes değerini 500-2000 olarak ayarlarsanız aralığın sonundaki değer geçersiz olur. Bu durumda OSS HTTP durum kodu 206 döndürür ve 500. bayt ile 999. bayt arasındaki verileri sunar.

  • Range: bytes değerini 1000-2000 olarak ayarlarsanız aralığın başındaki değer geçersiz olur. Bu durumda OSS HTTP durum kodu 416 ve InvalidRange hata kodunu döndürür.

Aşağıdaki örnek kod, aralığa göre veri indirmek için standart davranışların nasıl belirtileceğini göstermektedir.

package main

import (
	"io"
	"log"
	"strings"

	"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("Error: %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("Error: %v", err)
	}

	// Specify the name of the bucket. 
	bucketName := "yourBucketName"
	bucket, err := client.Bucket(bucketName)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	// Upload 1,000 bytes. 
	strContent := ""
	for i := 0; i < 100; i++ {
		strContent += "abcdefghij"
	}
	log.Printf("content len: %d\n", len(strContent))

	// Upload the string. 
	objectName := "yourObjectName"
	err = bucket.PutObject(objectName, strings.NewReader(strContent))
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	// If you specify a range from byte 500 to byte 2000, the value at the end of the range is invalid. Then, OSS returns HTTP status code 206 and the data that is within the range from byte 500 to byte 999. 
	// If you specify a range from byte 1000 to byte 2000, the value at the start of the range is invalid. Then, OSS returns HTTP status code 416 and error code InvalidRange. 
	rangeStart := int64(1000)
	rangeEnd := int64(2000)
	body, err := bucket.GetObject(objectName, oss.Range(rangeStart, rangeEnd), oss.RangeBehavior("standard"))
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	defer body.Close()

	// Read the data and load it into the memory.
	data, err := io.ReadAll(body)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	if len(data) != 500 {
		log.Fatalf("read data error, len: %d", len(data))
	}
	log.Printf("data: %s", string(data))
}

İlgili Konular

  • Aralık indirerek indirme işlemini gerçekleştirmek için kullanılan tam örnek kodu görüntülemek üzere GitHub adresini ziyaret edin.

  • Aralık indirerek indirme işlemini gerçekleştirmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetObject.