我尝试从 s3 中的旧文件恢复文件,前提是该文件不存在。 我当前的代码是这样的:
import botocore
import os
s3 = boto3.resource('s3')
def checkFile(filename, buckett="selfmarkett"):
try:
s3.Object(buckett, filename).load()
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
try:
_filename, _extension = os.path.splitext(filename)
oldFilename = _filename + ".p_up" + _extension
s3.Object(buckett, filename).copy_from(CopySource=oldFilename)
except Exception as error:
print("Error2:", error)
我的问题是,当脚本尝试使用函数 copy_from 时,返回此错误:
NoSuchBucket: An error occurred (NoSuchBucket) when calling the CopyObject operation: The specified bucket does not exist
存储桶已在对象中指定,我如何修复此错误?
我遇到了同样的问题,我通过在 CopySource 的路径中添加存储桶名称来修复它,如下所示:
s3.Object(bucket, "new_folder/new_file").copy_from(CopySource="<bucket>/old_folder/old_file")
我不熟悉您正在编码的语言,但是是否可以为 copy_from 提供第二个参数?我怀疑您需要在调用 copy_from 时指定存储桶和文件名/密钥
与nodeJS api有同样的问题,它可能与这里相同:https://stackoverflow.com/a/66492490/2083748(您需要对复制源进行URL编码)
我已经检查过这里的文档: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/copy.html 我已经使用 boto3.resource 达到了这两种工作方法:
s3r = boto3.resource('s3')
copy_source = {
'Bucket': BUCKET_ORIGIN,
'Key': originFile
}
s3r.meta.client.copy(copy_source, BUCKET_DESTINATION, destination)
并使用boto3.client:
s3c = boto3.client('s3')
s3c.copy_object(Bucket = BUCKET_ORIGIN, CopySource = BUCKET_DESTINATION + "/" + originFile, Key = destination)
两个桶可以是相同的(在我的例子中)