Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Conditional downloads (Browser.js SDK)

Son Güncelleme:Oct 01, 2026

Bir dosyayı (nesne) indirirken bir veya daha fazla koşul belirtebilirsiniz. Dosya yalnızca belirtilen koşullar karşılandığında indirilir. Koşullar karşılanmazsa bir hata döner ve indirme başlamaz.

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.

  • 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ıdan 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 amacıyla OSS'e erişmek için Güvenlik Belirteç Hizmeti'nden (STS) alınan geçici erişim kimlik bilgilerini kullanmanızı öneririz.

    Geçici erişim kimlik bilgileri; bir AccessKey çifti ve bir güvenlik belirtecinden oluşur. AccessKey çifti, bir AccessKey ID ve bir AccessKey Secret'tan meydana gelir. Geçici erişim kimlik bilgilerini nasıl alacağınız hakkında daha fazla bilgi için bkz. Geçici erişim yetkilendirmesi için STS kullanma.

Örnek kod

Aşağıdaki kod, koşullu bir indirme işlemini gösterir:

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

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

<body>
  <button id='upload'>Upload</button>
  <button id='download'>Download</button>
    <!--Import the SDK file.-->
  <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js"></script>
  <script type="text/javascript">
      const client = new OSS({
         // Set region to the region where the bucket is located. For example, if the 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 SecurityToken obtained from STS.
        stsToken: 'yourSecurityToken',
        // Specify the bucket name. For example, examplebucket.
        bucket: "examplebucket",
      });

    const download = document.getElementById('download')
    const upload = document.getElementById('upload')

    // Upload the file.  
    upload.addEventListener('click', () => {
      // Specify the content of the file to upload.
      const file = new Blob(['examplecontent'])
      // Specify the full path of the file to upload. For example, exampledir/exampleobject.txt.
      const fileName = 'exampledir/exampleobject.txt'
      const result = client.put(fileName, file).then(r => console.log(r))
    })

    // Download the file.
    download.addEventListener('click', () => {
      // Specify the byte range to download.
      const start = 1, end = 5
      client.get('exampledir/exampleobject.txt', {
        headers: {
          // Specify a time in the If-Modified-Since request header. The file is downloaded if the specified time is earlier than the actual modification time of the file. If the specified time is the same as or later than the actual modification time, 304 Not Modified is returned.
          "If-Modified-Since": new Date("1970-01-01").toGMTString()
          // Specify a time in the If-Unmodified-Since request header. The file is downloaded if the specified time is the same as or later than the actual modification time of the file. If the specified time is earlier than the actual modification time, 412 Precondition Failed is returned.
          //"If-Unmodified-Since": new Date(1970-01-01).toGMTString()
          // Specify an ETag in the If-Match request header. The file is downloaded if the specified ETag matches the ETag of the file. If the specified ETag does not match, 412 Precondition Failed is returned.
          //"If-Match": '5B3C1A2E0563E1B002CC607C****'
          // Specify an ETag in the If-None-Match request header. The file is downloaded if the specified ETag does not match the ETag of the file. If the specified ETag matches, 304 Not Modified is returned.
          //"If-None-Match": '5B3C1A2E0563E1B002CC607C****'
        },
      }).then(r => {
        if (r.content.length > 0) {                
          const newBlob = new Blob([r.content], { type: r.res.headers['content-type'] });
          const link = document.createElement('a')
          link.href = window.URL.createObjectURL(newBlob)
          link.download = 'foo.txt'
          link.click()
          window.URL.revokeObjectURL(link.href)
        } else {
          console.log('Error code', r.res.status)
          console.log('No items to download that meet the conditions')
        }
      })
    })

  </script>
</body>

</html>

İlgili bilgiler

  • Koşullu indirmeler için eksiksiz örnek kodu görmek üzere GitHub örneğine bakın.

  • API işlemi hakkında daha fazla bilgi için bkz. GetObject.