Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Range download (Browser.js SDK)

Son Güncelleme:Oct 01, 2026

Bir dosyanın yalnızca belirli bir bölümüne ihtiyacınız varsa, aralık indirme yöntemiyle belirtilen aralıktaki verileri alabilirsiniz.

Kullanım notları

  • Webpack ve Browserify gibi paketleme araçlarını kullanırken, npm install ali-oss komutunu çalıştırarak Browser.js için OSS SDK'yı yükleyin.

  • Bir tarayıcıdan bir OSS paketine erişmek istiyorsanız ancak paket için CORS kuralları yapılandırılmamışsa tarayıcı isteği reddeder. Bu nedenle, bir pakete tarayıcı üzerinden erişmek istiyorsanız paket için CORS kurallarını yapılandırmanız gerekir. Daha fazla bilgi için bkz. Kurulum.

  • Browser.js için OSS SDK çoğunlukla tarayıcılarda kullanılır. AccessKey çiftinizin açığa çıkmasını önlemek için OSS'a erişirken Security Token Service (STS) üzerinden alınan geçici kimlik bilgilerini kullanmanızı öneririz.

    Geçici kimlik bilgileri; bir AccessKey çifti ve bir güvenlik belirtecinden oluşur. AccessKey çifti, bir AccessKey ID ve bir AccessKey Secret içerir. Geçici kimlik bilgilerini alma hakkında daha fazla bilgi için bkz. Geçici erişim yetkilendirmesi için STS kullanma.

Örnek kod

Aşağıdaki örnek kod, aralık indirme işleminin nasıl yapılacağını gösterir:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Document</title>
</head>

<body>
  <button id='download'>Download</button>
  <!--Import the SDK file.-->
  <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"></script>
  <script type="text/javascript">    
    const client = new OSS({
      // Set region to the region where your bucket is located. For example, if your bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
      region: 'yourRegion',
      authorizationV4: true,
      // The temporary AccessKey ID and AccessKey secret obtained from Security Token Service (STS).
      accessKeyId: 'yourAccessKeyId',
      accessKeySecret: 'yourAccessKeySecret',
      // The security token (SecurityToken) obtained from STS.
      stsToken: 'yourSecurityToken',
      // Specify the bucket name. For example, examplebucket.
      bucket: "examplebucket",
    });

    // Get the DOM element.
    const download = document.getElementById('download')
    // For a 1000-byte file, the valid byte range is 0 to 999.
    // If the specified range is invalid, for example, the start or end position is a negative value or is greater than the file size, the entire file is downloaded.
    const start = 0, end = 999
    // Add a listener to the button.
    download.addEventListener('click', () => {
      client.get('example.txt',{
        headers: {          
          Range: `bytes=${start}-${end}`,
        },
      }).then(r => {
        // Convert the returned data into a downloadable object in BLOB format.
        const newBlob = new Blob([r.content], { type: r.res.headers['content-type'] });
        // Create an anchor element.
        const link = document.createElement('a')
        // Attach the href property to the element.
        link.href = window.URL.createObjectURL(newBlob)
        // Specify the name of the local file.
        link.download = 'example.txt'
        // Download the object.
        link.click()
        // Remove the attached URL.
        window.URL.revokeObjectURL(link.href)
      })
    })

  </script>
</body>

</html>

İlgili kaynaklar

  • Aralık indirme işlemlerine ilişkin eksiksiz örnek kod için GitHub örneğine

  • Aralık indirme işlemlerine ilişkin API referansı için bkz. GetObject.