Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Change the storage class of a file (Browser.js SDK)

Son Güncelleme:Oct 01, 2026

Object Storage Service (OSS), sıcak verilerden soğuk verilere kadar çeşitli veri depolama senaryolarını karşılamak üzere Standart, Seyrek Erişim (IA), Arşiv, Soğuk Arşiv ve Derin Soğuk Arşiv depolama sınıflarını sunar. OSS'de bir nesne oluşturulduktan sonra içeriği değiştirilemez. Bir nesnenin depolama sınıfını dönüştürmek için Bucket.CopyObject yöntemini kullanarak nesneyi kopyalayın; bu işlem yeni bir nesne oluşturur ve yeni nesnenin depolama sınıfını belirlemenize olanak tanır.

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 OSS paketine erişmek istediğinizde 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'ye erişirken Security Token Service (STS) üzerinden alınan geçici erişim kimlik bilgilerini kullanmanızı öneririz.

    Geçici erişim kimlik bilgileri bir AccessKey çiftinden ve bir güvenlik belirtecinden oluşur. AccessKey çifti bir AccessKey ID ve bir AccessKey Secret içerir. Geçici erişim kimlik bilgilerinin nasıl alınacağı hakkında daha fazla bilgi için bkz. Geçici erişim yetkilendirmesi için STS kullanma.

Örnek kod

Bir nesnenin depolama sınıfını Standart veya Seyrek Erişim'den Arşiv'e dönüştürme

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

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

<body>
  <button id='upload'>Upload</button>
  <button id='copy'>Convert object storage class</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 yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou.
       region: 'yourRegion',
       authorizationV4: true,
       // The temporary AccessKey ID and AccessKey secret obtained from STS.
       accessKeyId: 'yourAccessKeyId',
       accessKeySecret: 'yourAccessKeySecret',
       // The security token obtained from STS.
       stsToken: 'yourSecurityToken',
       // Specify the bucket name. Example: examplebucket.
       bucket: "examplebucket",
     });

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

    // Specify the content of the file to upload.
    const file = new Blob(['examplecontent'])
    // Specify the full path of the object to upload to examplebucket. Example: exampledir/exampleobject.txt.
    const fileName = 'exampledir/exampleobject.txt'

    // Upload the file.
    upload.addEventListener('click', () => {
      client.put(fileName, file).then(r => console.log(r))
    })

    // Convert the object storage class using the copy method.
    copy.addEventListener('click', () => {
      // Specify the name of the destination object. Example: newexampleobject.txt.
      client.copy('newexampleobject.txt', fileName, {
        headers: {
          // Set the storage class of the object to Archive. To change the storage class to Cold Archive, replace Archive with ColdArchive.
          'x-oss-storage-class': 'Archive'
        }
      }
      ).then(r => {
        console.log(r.res.status)
      })
    })

  </script>
</body>

</html>

Bir nesnenin depolama sınıfını Arşiv'den Seyrek Erişim veya Standart'a dönüştürme

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
  </head>

  <body>
    <button id="check">Check</button>
    <button id="restore">Restore</button>
    <button id="change">Convert</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 yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou.
        region: 'yourRegion',
        // The temporary AccessKey ID and AccessKey secret obtained from STS.
        accessKeyId: 'yourAccessKeyId',
        accessKeySecret: 'yourAccessKeySecret',
        // The security token obtained from STS.
        stsToken: 'yourSecurityToken',
        // Specify the bucket name. Example: examplebucket.
        bucket: "examplebucket",
     });
 
      const check = document.getElementById("check");
      const change = document.getElementById("change");
      const restore = document.getElementById("restore");

      check.addEventListener("click", () => {
        console.log("Check the object type");
        // Check the Header in the developer tools to confirm the object storage class.
        client.head("srcobject.txt").then((r) => console.log(r));
      });
      // Restore the object.
      restore.addEventListener("click", () => {
        // Specify the name of the object to restore. Example: srcobject.txt.
        client.restore("srcobject.txt").then((r) => {
          console.log(r);
          console.log("Start restoring");
        });
      });

      // After the object is restored, convert its storage class.
      change.addEventListener("click", () => {
        // The restoration time depends on the object size.
        console.log("Start converting");
        client
          // Specify destobject.txt as the name of the destination object after srcobject.txt is copied.
          .copy("destobject.txt", "srcobject.txt", {
            // Set the storage class of the object to Infrequent Access (IA). To change the storage class to Standard, replace IA with Standard.
            headers: { "x-oss-storage-class": "IA" },
          })
          .then((r) => console.log(r))
          // A common cause for conversion errors is that the object is still being restored.
          .catch((e) => console.log("Conversion error:", e));
      });
    </script>
  </body>
</html>
                    

İlgili kaynaklar

  • Nesne depolama sınıflarının dönüştürülmesine ilişkin tam örnek kodlar için bkz. GitHub örnekleri.

  • Nesne depolama sınıflarını dönüştüren API işlemi hakkında daha fazla bilgi için bkz. CopyObject.