如何使用他们的 sdk 在 AWS S3 中移动(而不是复制)文件?
How can I move (not copy) a file in AWS S3 using their sdk?
我需要将 AWS s3 存储桶中的文件移动到另一个位置,例如:
我查看了文档:https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html,但没有发现任何关于移动或更新文件的提及(我想我可以更新文件密钥路径...)。
到目前为止,我似乎需要复制文件然后删除旧文件?有更直接的方法吗?
我当前复制然后删除旧文件的代码:
function moveFileInAws(fromLocation, toLocation, callback) {
awsSdk.copyObject({
Bucket: BUCKET_NAME,
ACL: 'public-read',
CopySource: fromLocation,
Key: toLocation
}, (err, data) => {
if (err) {
console.log(err)
return callback("Couldn't copy files in directory")
}
// callback()
awsSdk.deleteObject({ Key: fromLocation }, (err, data) => {
if (err) {
console.log("Couldn't delete files in directory")
console.log(err)
return callback("Couldn't delete files in directory")
}
callback()
})
})
}
Amazon S3 不提供 API 将对象从一个存储桶移动或重命名到另一个存储桶的功能。
如您的示例所示,您可以使用 copyObject
和 deleteObject
来移动或重命名对象,方法是首先将对象复制到新名称(您可以使用相同的存储桶作为源和目标),然后从其旧位置删除对象。
有关详细信息,请参阅 Performing Operations on Amazon S3 Objects
基于此处的答案: 其中用户@Michael-sqlbot 评论:
That's because S3, itself, doesn't have an atomic move or rename operation... so there isn't really an alternative
以及此处的文档:https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-s3-objects.html#copy-object(针对 Java SDK,但在这里似乎很有用)其中注释:
You can use copyObject with deleteObject to move or rename an object, by first copying the object to a new name (you can use the same bucket as both the source and destination) and then deleting the object from its old location.
听起来 S3 的基础架构根本不支持在单个操作中移动或重命名。您必须复制,然后删除。
我一点都不熟悉您正在使用的 JavaScript SDK,但是使用 aws cli,有:
aws s3 mv s3://bucket/folder/file s3://bucket/folder2/file
看来它会做你想做的事。不确定 JavaScript SDK。
我需要将 AWS s3 存储桶中的文件移动到另一个位置,例如:
我查看了文档:https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html,但没有发现任何关于移动或更新文件的提及(我想我可以更新文件密钥路径...)。
到目前为止,我似乎需要复制文件然后删除旧文件?有更直接的方法吗?
我当前复制然后删除旧文件的代码:
function moveFileInAws(fromLocation, toLocation, callback) {
awsSdk.copyObject({
Bucket: BUCKET_NAME,
ACL: 'public-read',
CopySource: fromLocation,
Key: toLocation
}, (err, data) => {
if (err) {
console.log(err)
return callback("Couldn't copy files in directory")
}
// callback()
awsSdk.deleteObject({ Key: fromLocation }, (err, data) => {
if (err) {
console.log("Couldn't delete files in directory")
console.log(err)
return callback("Couldn't delete files in directory")
}
callback()
})
})
}
Amazon S3 不提供 API 将对象从一个存储桶移动或重命名到另一个存储桶的功能。
如您的示例所示,您可以使用 copyObject
和 deleteObject
来移动或重命名对象,方法是首先将对象复制到新名称(您可以使用相同的存储桶作为源和目标),然后从其旧位置删除对象。
有关详细信息,请参阅 Performing Operations on Amazon S3 Objects
基于此处的答案:
That's because S3, itself, doesn't have an atomic move or rename operation... so there isn't really an alternative
以及此处的文档:https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-s3-objects.html#copy-object(针对 Java SDK,但在这里似乎很有用)其中注释:
You can use copyObject with deleteObject to move or rename an object, by first copying the object to a new name (you can use the same bucket as both the source and destination) and then deleting the object from its old location.
听起来 S3 的基础架构根本不支持在单个操作中移动或重命名。您必须复制,然后删除。
我一点都不熟悉您正在使用的 JavaScript SDK,但是使用 aws cli,有:
aws s3 mv s3://bucket/folder/file s3://bucket/folder2/file
看来它会做你想做的事。不确定 JavaScript SDK。