Copiez des objets au sein d'un même bucket ou entre des buckets situés dans la même région.
Notes
Si vous utilisez des outils de regroupement tels que Webpack et Browserify, installez le SDK OSS pour Browser.js en exécutant la commande npm install ali-oss.
-
Le SDK OSS pour Browser.js s'utilise principalement dans les navigateurs. Pour éviter d'exposer votre paire de clés AccessKey, nous vous recommandons d'utiliser des identifiants d'accès temporaires obtenus via Security Token Service (STS) pour accéder à OSS.
Les identifiants d'accès temporaires se composent d'une paire AccessKey et d'un jeton de sécurité. La paire AccessKey comprend un AccessKey ID et un AccessKey Secret. Pour plus d'informations sur l'obtention d'identifiants d'accès temporaires, consultez Utiliser STS pour une autorisation d'accès temporaire.
Les buckets source et destination impliqués dans l'opération de copie doivent disposer de règles CORS (Cross-Origin Resource Sharing) configurées. Pour plus d'informations, consultez Préparatifs.
Permissions
Par défaut, un compte Alibaba Cloud dispose de toutes les permissions. Les utilisateurs RAM ou les rôles RAM associés à un compte Alibaba Cloud ne disposent d'aucune permission par défaut. Le compte Alibaba Cloud ou l'administrateur du compte doit accorder les permissions d'opération via des politiques RAM ou une Bucket Policy.
|
API |
Action |
Description |
|
CopyObject |
|
Copie des objets au sein d'un bucket ou entre des buckets situés dans la même région. |
|
|
||
|
|
Si vous spécifiez la version de l'objet source via versionId, cette permission est également requise. |
|
|
|
Si vous copiez les tags d'objet via x-oss-tagging, ces permissions sont nécessaires. |
|
|
|
||
|
|
Si vous spécifiez les tags d'une version spécifique de l'objet source via versionId, cette permission est également requise. |
|
|
|
Lors de la copie d'un objet, si les métadonnées de l'objet de destination contiennent X-Oss-Server-Side-Encryption: KMS, ces deux permissions sont requises. |
|
|
|
Copier un objet au sein d'un bucket
Le code suivant illustre la copie de l'objet srcobject.txt vers destobject.txt au sein du bucket examplebucket.
<!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>
Copier un objet entre des buckets
Le code suivant montre comment copier l'objet srcobject.txt depuis le bucket source srcbucket vers l'objet destobject.txt dans le bucket de destination destbucket.
<!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>
Références
Pour obtenir l'exemple de code complet relatif à la copie d'un objet, consultez les exemples GitHub.
Pour plus d'informations sur l'opération API de copie d'un objet, consultez CopyObject.