我有python代码,可将json文件上传到AWS的s3存储桶中。下面是有效的代码:
ACCESS_KEY = '##########'
SECRET_KEY = '######################/####'
def upload_to_aws(schoolID, bucketName):
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY)
try:
s3.upload_file(schoolID, bucketName, schoolID)
print(schoolID + " Upload Successful")
return True
except FileNotFoundError:
print(schoolID + " The file was not found")
return False
except NoCredentialsError:
print(schoolID + " Credentials not available")
return False
uploaded = upload_to_aws(schoolID + '.json', 'bucketName')
但是,我现在想将其上传到s3存储桶中的特定文件夹。我尝试在上面的代码中编辑以下行:
uploaded = upload_to_aws(schoolID + '.json', 'bucketName\AcademicYears')
但是我得到了这个错误:
ParamValidationError: Parameter validation failed:
Invalid bucket name "bucketName\AcademicYears": Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$" or be an ARN matching the regex "^arn:(aws).*:s3:[a-z\-0-9]+:[0-9]{12}:accesspoint[/:][a-zA-Z0-9\-]{1,63}$"
有人知道我可以如何更改此代码以使其工作?预先感谢
答案 0 :(得分:0)
您似乎正在向存储桶中添加一个字符串,而AWS拒绝该字符串。子目录已添加到存储桶键。
如果您的存储桶名为“ bucketName”,则这是顶级目录。此目录下的所有内容都会添加到密钥中。
例如:
bucketName <-桶
AcademicYears <-子目录
尝试:
uploaded = upload_to_aws('AcademicYears/' + schoolID + '.json', 'bucketName')