Aynı bölgedeki paketler arasında veya bir paket içindeki nesneleri kopyalayın.
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.
-
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şmek için Security Token Service'ten (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.
Kopyalama işlemi için kaynak ve hedef paketlerde çapraz kaynak paylaşımı (CORS) kuralları yapılandırılmış olmalıdır. Daha fazla bilgi için bkz. Hazırlıklar.
İzinler
Varsayılan olarak bir Alibaba Cloud hesabı tüm 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çindeki veya aynı bölgedeki paketler arasındaki nesneleri kopyalar. |
|
|
||
|
|
versionId aracılığıyla kaynak nesne sürümünü belirtirseniz bu izin de gereklidir. |
|
|
|
x-oss-tagging aracılığıyla nesne etiketlerini kopyalarsanız bu izinler gereklidir. |
|
|
|
||
|
|
versionId aracılığıyla kaynak nesnenin belirli bir sürümünün etiketlerini belirtirseniz bu izin de gereklidir. |
|
|
|
Bir nesneyi kopyalarken hedef nesne meta verileri X-Oss-Server-Side-Encryption: KMS içeriyorsa bu iki izin gereklidir. |
|
|
|
Bir paket içindeki bir nesneyi kopyalama
Aşağıdaki kod, srcobject.txt nesnesinin examplebucket paketi içinde destobject.txt olarak nasıl kopyalanacağını gösterir.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button id='upload'>Upload</button>
<button id='copy'>Copy</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({
// Specify 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 object to upload.
const file = new Blob(['examplecontent'])
// Specify the full path of the object to upload. Example: srcobject.txt.
const fileName = 'srcobject.txt'
// Upload the object.
upload.addEventListener('click', () => {
const result = client.put(fileName, file).then(r => console.log(r))
})
// Copy the object.
copy.addEventListener('click', () => {
// Specify the name of the destination object.
client.copy('destobject.txt', fileName
// Set the HTTP headers and custom metadata for the destination object.
//{
// Specify the headers parameter to set the HTTP headers for the destination object. If you do not specify this parameter, the destination object has the same HTTP headers as the source object.
// headers:{
//'Cache-Control': 'no-cache',
// The copy operation is performed only if the ETag of the source object matches the specified ETag.
//'if-match': '5B3C1A2E053D763E1B002CC607C5****',
// The copy operation is performed only if the ETag of the source object does not match the specified ETag.
//'if-none-match': '5B3C1A2E053D763E1B002CC607C5****',
// The copy operation is performed only if the source object has been modified since the specified time.
//'if-modified-since': '2021-12-09T07:01:56.000Z',
// The copy operation is performed only if the source object has not been modified since the specified time.
//'if-unmodified-since': '2021-12-09T07:01:56.000Z',
// Specify the access control list (ACL) for the destination object. In this example, the ACL is set to private. This means only the object owner and authorized users have read and write permissions. Other users cannot access the object.
//'x-oss-object-acl': 'private',
// Specify the tags for the object. You can specify multiple tags.
//'x-oss-tagging': 'Tag1=1&Tag2=2',
// Specify whether to overwrite a destination object that has the same name. In this example, the value is set to true to prevent overwriting.
//'x-oss-forbid-overwrite': 'true',
//},
// Specify the meta parameter to customize the metadata of the destination object. If you do not specify this parameter, the destination object has the same metadata as the source object.
// meta:{
// location: 'hangzhou',
// year: 2015,
// people: 'mary'
//}
// }
).then(r => {
console.log(r.res.status)
})
})
</script>
</body>
</html>
Paketler arasında bir nesneyi kopyalama
Aşağıdaki kod, srcobject.txt nesnesinin srcbucket kaynak paketinden destbucket hedef paketindeki destobject.txt nesnesine nasıl kopyalanacağını gösterir.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<button id="copy">Copy</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({
// Specify 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 destination bucket name.
bucket: "destbucket",
});
const copy = document.getElementById("copy");
copy.addEventListener("click", () => {
client
.copy(
// Specify the name of the destination object.
"srcobject.txt",
// Specify the name of the source object.
"destobject.txt",
// Specify the source bucket name.
"srcbucket"
)
.then((r) => console.log(r));
});
</script>
</body>
</html>
İlgili konular
Bir nesneyi kopyalamaya ilişkin eksiksiz örnek kod için bkz. GitHub örnekleri.
Bir nesneyi kopyalamaya yönelik API işlemi hakkında daha fazla bilgi için bkz. CopyObject.