Aynı bölgedeki bir paket içinde veya paketler arasında nesneleri kopyalayın.
Notlar
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek için dahili uç noktayı kullanın. Desteklenen bölgeler ve uç noktalar hakkında ayrıntılı bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınır. Daha fazla bilgi için bkz. Erişim Kimlik Bilgilerini Yapılandırma.
Bu konu, bir OSS uç noktası kullanarak bir OSSClient örneği oluşturmayı göstermektedir. Özel alan adı kullanımı veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. İstemci yapılandırması.
Kaynak nesne üzerinde okuma izniniz ve hedef paket üzerinde okuma/yazma izniniz olmalıdır.
Kaynak ve hedef paketlerde bekletme ilkeleri yapılandırılmamış olmalıdır. Aksi takdirde kopyalama işlemi başarısız olur ve Belirttiğiniz nesne değiştirilemez. hatası döndürülür.
Bölgeler arası kopyalama desteklenmez. Örneğin, Çin (Hangzhou) bölgesindeki bir paketten Çin (Qingdao) bölgesindeki bir pakete nesne kopyalayamazsınız.
İzinler
Varsayılan olarak bir Alibaba Cloud hesabı tam izinlere sahiptir. Bir Alibaba Cloud hesabı altındaki RAM kullanıcıları veya RAM rolleri varsayılan olarak herhangi bir izne sahip değildir. Alibaba Cloud hesabı veya hesap yöneticisi, işlem izinlerini RAM ilkeleri veya Paket İlkesi aracılığıyla vermelidir.
|
API |
Eylem |
Açıklama |
|
CopyObject |
|
Bir paket içinde veya aynı bölgedeki paketler arasında nesneleri kopyalar. |
|
|
||
|
|
Kaynak nesne sürümünü versionId üzerinden belirtirseniz bu izin de gereklidir. |
|
|
|
Nesne etiketlerini x-oss-tagging üzerinden kopyalarsanız bu izinler gereklidir. |
|
|
|
||
|
|
Kaynak nesnenin belirli bir sürümünün etiketlerini versionId üzerinden belirtirseniz bu izin de gereklidir. |
|
|
|
Bir nesneyi kopyalarken, hedef nesne meta verileri X-Oss-Server-Side-Encryption: KMS içeriyorsa bu iki izin gereklidir. |
|
|
|
Küçük bir nesneyi kopyalama
Boyutu 1 GB'ı aşmayan bir nesneyi aynı paket içinde veya aynı bölgedeki farklı bir pakete kopyalamak için copy yöntemini kullanabilirsiniz.
-
Aynı paket içinde bir nesneyi kopyalama
Aşağıdaki örnek kod, aynı paket içinde bir nesnenin nasıl kopyalanacağını göstermektedir.
const OSS = require('ali-oss'); const client = new OSS({ // 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 oss-cn-hangzhou. region: 'yourRegion', // 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. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the name of the bucket. Example: examplebucket. bucket: 'examplebucket', // Specify whether to enable HTTPS. If you set secure to true, HTTPS is enabled. // secure: true }) // Copy an object within a bucket. async function copySmallObjecInSameBucket() { try { // Specify the full paths of the destination object and source object. Do not include the bucket name in the full paths. // Specify HTTP headers and custom metadata for the destination object. const result = await client.copy('destexampleobject.txt', 'srcexampleobject.txt', { // Configure the headers parameter to specify HTTP headers for the destination object. If you do not configure the headers parameter, the HTTP headers of the destination object are the same as the HTTP headers of the source object. The HTTP headers of the source object are copied. headers: { 'Cache-Control': 'no-cache', // If the ETag value of the source object is the same as the ETag value specified in the request, OSS copies the object and returns 200 OK. 'if-match': '5B3C1A2E053D763E1B002CC607C5****', // If the ETag value that you specify in the request is different from the ETag value of the source object, OSS copies the object and returns 200 OK. 'if-none-match': '5B3C1A2E053D763E1B002CC607C5****', // If the time that is specified in the request is earlier than the time when the object is modified, OSS copies the object and returns 200 OK. 'if-modified-since': '2021-12-09T07:01:56.000Z', // If the source object has not been modified since the specified time, the object is copied and 200 OK is returned. 'if-unmodified-since': '2021-12-09T07:01:56.000Z', // Specify the access control list (ACL) of the destination object. In this example, the ACL is set to private, which indicates that only the object owner and authorized users have read and write permissions on the object. Other users do not have permissions to access the object. 'x-oss-object-acl': 'private', // Specify tags for the object. You can specify multiple tags for the object at the same time. 'x-oss-tagging': 'Tag1=1&Tag2=2', // Specify whether the CopyObject operation overwrites an existing object that has the same name. In this example, this parameter is set to true, which specifies that the CopyObject operation does not overwrite an existing object that has the same name. 'x-oss-forbid-overwrite': 'true', }, // Configure the meta parameter to specify the metadata of the destination object. If you do not configure the meta parameter, the metadata of the destination object is the same as the metadata of the source object. The metadata of the source object is copied. meta: { location: 'hangzhou', year: 2015, people: 'mary', }, }); console.log(result); } catch (e) { console.log(e); } } copySmallObjecInSameBucket() -
Aynı bölgedeki paketler arasında bir nesneyi kopyalama
Aşağıdaki örnek kod, aynı bölgedeki paketler arasında bir nesnenin nasıl kopyalanacağını göstermektedir.
const OSS = require('ali-oss'); const client = new OSS({ // 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 oss-cn-hangzhou. region: 'yourRegion', // 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. accessKeyId: process.env.OSS_ACCESS_KEY_ID, accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET, authorizationV4: true, // Specify the name of the destination bucket. bucket: 'destexamplebucket', }); async function copySmallObjectBetweenBuckets() { try { // Specify the name of the destination object destobject.txt, the name of the source object srcobject.txt, and the name of the bucket to which the source object belongs. const result = await client.copy('destobject.txt', 'srcobject.txt', 'srcbucket', { // Configure the headers parameter to specify HTTP headers for the destination object. If you do not configure the headers parameter, the HTTP headers of the destination object are the same as the HTTP headers of the source object. The HTTP headers of the source object are copied. headers: { 'Cache-Control': 'no-cache', }, // Configure the meta parameter to specify the metadata of the destination object. If you do not configure the meta parameter, the metadata of the destination object is the same as the metadata of the source object. The metadata of the source object is copied. meta: { location: 'hangzhou', year: 2015, people: 'mary', }, }); console.log(result); } catch (e) { console.log(e); } } copySmallObjectBetweenBuckets()
Büyük bir nesneyi kopyalama
1 GB'tan büyük nesneler için çok parçalı kopyalama gerçekleştirmek üzere multipartUploadCopy yöntemini kullanabilirsiniz.
Aşağıdaki örnek kod, aynı bölgedeki paketler arasında bir nesnenin nasıl kopyalanacağını göstermektedir.
const OSS = require("ali-oss");
const client = new OSS({
// 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 oss-cn-hangzhou.
region: 'yourregion',
// 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.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the name of the destination bucket.
bucket: "destexamplebucket",
});
async function copyLargeObjectBetweenDifferentBuckets() {
try {
const copyheaders = {
// If the ETag value of the source object is the same as the ETag value that is specified in the request, OSS copies the object. Otherwise, OSS returns the HTTP status code 412 (PreconditionFailed).
"x-oss-copy-source-if-match": "5B3C1A2E053D763E1B002CC607C5****",
// If the ETag value of the source object is different from the ETag value that is specified in the request, OSS copies the object and returns 200 OK. Otherwise, OSS returns the HTTP status code 304 (NotModified).
"x-oss-copy-source-if-none-match": "5B3C1A2E053D763E1B002CC607C5****",
// If the time that is specified in the request is later than or the same as the time when the object is modified, OSS copies the object and returns 200 OK. Otherwise, OSS returns the HTTP status code 412 (PreconditionFailed).
"x-oss-copy-source-if-unmodified-since": "2022-12-09T07:01:56.000Z",
// If the time that is specified in the request is earlier than the time when the object is modified, OSS copies the object and returns 200 OK. Otherwise, OSS returns the HTTP status code 304 (NotModified).
"x-oss-copy-source-if-modified-since": "2022-12-09T07:01:56.000Z",
};
const headers = {
// Specify the caching behavior of the web page when the object is downloaded.
"Cache-Control": "no-cache",
// Specify the name of the object when the object is downloaded.
"Content-Disposition": "somename",
// Specify the expiration time in milliseconds.
Expires: "1000",
};
let savedCpt;
// Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destexampleobject1.txt.
const r1 = await client.multipartUploadCopy("destexampleobject1.txt", {
// Specify the full path of the source object. Do not include the bucket name in the full path. Example: srcexampleobject.txt.
sourceKey: "srcexampleobject.txt",
// Specify the name of the source bucket. Example: sourcebucket.
sourceBucketName: "sourcebucket",
copyheaders: copyheaders,
});
console.log(r1);
// Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destexampleobject2.txt.
const r2 = await client.multipartUploadCopy("destexampleobject2.txt", {
// Specify the full path of the source object. Do not include the bucket name in the full path. Example: srcexampleobject.txt.
sourceKey: "srcexampleobject.txt",
// Specify the name of the source bucket. Example: sourcebucket.
sourceBucketName: "sourcebucket",
}, {
// Set the number of parts that can be uploaded in parallel.
parallel: 4,
// Set the part size.
partSize: 1024 * 1024,
progress: function (p, cpt, res) {
console.log(p);
savedCpt = cpt;
console.log(cpt);
console.log(res.headers["x-oss-request-id"]);
},
headers: headers,
copyheaders: copyheaders,
});
console.log(r2);
// Specify the full path of the destination object. Do not include the bucket name in the full path. Example: destexampleobject3.txt.
const r3 = await client.multipartUploadCopy("destexampleobject3.txt", {
// Specify the full path of the source object. Do not include the bucket name in the full path. Example: srcexampleobject.txt.
sourceKey: "srcexampleobject.txt",
// Specify the name of the source bucket. Example: sourcebucket.
sourceBucketName: "sourcebucket",
}, {
checkpoint: savedCpt,
progress: function (p, cpt, res) {
console.log(p);
console.log(cpt);
console.log(res.headers["x-oss-request-id"]);
},
});
console.log(r3);
} catch (e) {
console.log(e);
}
}
copyLargeObjectBetweenDifferentBuckets()
İlgili Konular
-
Küçük bir nesneyi kopyalama
Küçük bir nesneyi kopyalamaya ilişkin eksiksiz örnek kod için bkz. GitHub örneği.
Küçük bir nesneyi kopyalamaya yönelik API işlemi hakkında daha fazla bilgi için bkz. CopyObject.
-
Büyük bir nesneyi kopyalama
Büyük bir nesneyi kopyalamaya ilişkin eksiksiz örnek kod için bkz. GitHub örneği.
Büyük bir nesneyi kopyalamaya yönelik API işlemi hakkında daha fazla bilgi için bkz. UploadPartCopy.