Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Prevent overwriting files with the same name (Browser.js SDK)

Son Güncelleme:Oct 01, 2026

Varsayılan olarak, mevcut bir nesneyle aynı ada sahip bir nesne yüklerseniz var olan nesnenin üzerine yazılır. Bu konuda, nesneleri kopyalarken veya basit yükleme ve çok parçalı yükleme işlemlerini yaparken aynı ada sahip nesnelerin mevcut nesnelerin üzerine yazılmasını önlemek için x-oss-forbid-overwrite istek üst bilgisinin nasıl yapılandırılacağı açıklanmaktadı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'sını 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'sı çoğunlukla tarayıcılarda kullanılır. AccessKey çiftinizin açığa çıkmasını önlemek amacıyla OSS'ye erişmek için Güvenlik Belirteci 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 içerir. Geçici erişim kimlik bilgilerini edinme hakkında daha fazla bilgi için bkz. Geçici erişim yetkilendirmesi için STS kullanma.

Basit yükleme

Aşağıdaki kod, basit yükleme sırasında aynı ada sahip bir nesnenin üzerine yazılmasının nasıl engelleneceğini gösterir:

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

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

<body>
  <button id='upload'>Upload</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 Security Token Service (STS).
       accessKeyId: 'yourAccessKeyId',
       accessKeySecret: 'yourAccessKeySecret',
       // The security token (SecurityToken) obtained from STS.
       stsToken: 'yourSecurityToken',
       // Specify the bucket name. Example: examplebucket.
       bucket: "examplebucket",
      });

    const upload = document.getElementById('upload')

    // Specify the local file to upload.
    const fileContent = Array(1024 * 1024 * 10).fill('a').join('')
    const fileName = 'example.txt'
    const file = new Blob([fileContent])
    // Specify whether to overwrite an object that has the same name. In this example, this parameter is set to true. This indicates that an object that has the same name cannot be overwritten. If an object with the same name exists, an error is reported.
    const headers = {
      'x-oss-forbid-overwrite': true
    }
    upload.addEventListener('click', () => {
      // Upload the file.
      client.put(fileName, file, { headers }).then(r => console.log(r))
    })

  </script>
</body>

</html>

Dosya kopyalama

Aşağıdaki kod, bir nesneyi kopyalarken aynı ada sahip bir nesnenin üzerine yazılmasının nasıl engelleneceğini gösterir:

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

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

<body>
  <button id='upload'>Upload</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 Security Token Service (STS).
       accessKeyId: 'yourAccessKeyId',
       accessKeySecret: 'yourAccessKeySecret',
       // The security token (SecurityToken) obtained from STS.
       stsToken: 'yourSecurityToken',
       // Specify the bucket name. Example: examplebucket.
       bucket: "examplebucket",
      });

    const upload = document.getElementById('upload')

    // Specify the file to copy.
    const fileContent = Array(1024 * 1024 * 5).fill('a').join('')
    const fileName = 'example.txt'
    const file = new Blob([fileContent])
    // Specify whether to overwrite an object that has the same name during the copy operation. In this example, this parameter is set to true. This indicates that an object that has the same name cannot be overwritten. If an object with the same name exists, an error is reported.
    const headers = {
      'x-oss-forbid-overwrite': true
    }
    upload.addEventListener('click', () => {
      // Copy the file.
      // Set the name of the destination file to dest.txt.
      client.copy("dest.txt",fileName,{headers}).then(r=>console.log(r))
    })

  </script>
</body>

</html>

Çok parçalı yükleme

Aşağıdaki kod, çok parçalı yükleme sırasında aynı ada sahip bir nesnenin üzerine yazılmasının nasıl engelleneceğini gösterir:

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

  <body>
    <button id="upload">Upload</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 Security Token Service (STS).
        accessKeyId: 'yourAccessKeyId',
        accessKeySecret: 'yourAccessKeySecret',
        // The security token (SecurityToken) obtained from STS.
        stsToken: 'yourSecurityToken',
        // Specify the bucket name. Example: examplebucket.
        bucket: "examplebucket",
      });

      const upload = document.getElementById("upload");

      // Specify the file to upload.
      const fileContent = Array(1024 * 1024 * 5)
        .fill("a")
        .join("");
      const fileName = "src.txt";
      const file = new Blob([fileContent]);
      // Specify whether to overwrite an object that has the same name. In this example, this parameter is set to true. This indicates that an object that has the same name cannot be overwritten. If an object with the same name exists, an error is reported.
      const headers = {
        "x-oss-forbid-overwrite": true,
      };
      upload.addEventListener("click", () => {
        // Perform a multipart upload.
        client
          .multipartUpload("dest.txt", file, { headers })
          .then((r) => console.log(r));
      });
    </script>
  </body>
</html>

İlgili kaynaklar

  • Basit yükleme yapmak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. PutObject.

  • Bir nesneyi kopyalamak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. CopyObject.

  • Browser.js SDK'sındaki multipartUpload yöntemi, çok parçalı yüklemeler için kullanılır ve aşağıdaki üç API işlemini kapsüller:

    • Çok parçalı yüklemeyi başlatmaya yönelik API işlemi hakkında daha fazla bilgi için bkz. InitiateMultipartUpload.

    • Parçaları yüklemeye yönelik API işlemi hakkında daha fazla bilgi için bkz. UploadPart.

    • Çok parçalı yüklemeyi tamamlamaya yönelik API işlemi hakkında daha fazla bilgi için bkz. CompleteMultipartUpload.