如何解决使用 python 2.7
我目前正在使用 boto2 将一些文件上传到 s3 存储桶中,其中对象根据我上传的对象的键(生成的)进行排序。我现在需要将此脚本从 boto2 迁移到 boto3,但文档有点混乱。 有人可以帮助进行所需的代码更改或我需要注意的差异吗?因为我正在尝试将 boto3 与 python 2.7 一起使用。(此时无法升级到 python 3)
需要迁移的使用boto2的部分脚本如下:
if not boto.config.has_section('Credentials'):
boto.config.add_section('Credentials')
boto.config.set('Credentials','aws_access_key_id','%aws_access_key_id%')
boto.config.set('Credentials','aws_secret_access_key','%aws_secret_access_key%')
# Connect to S3
self.s3conn = boto.connect_s3()
self.bucket = self.s3conn.get_bucket('%aws_bucket%')
self.prefix = "%aws_prefix%"
self.manifest_prefix = "%aws_manifest%"
self.domain = "%aws_domain%"
self.s3_keys = []
self.uploadTrack = {"i": 0,"total": 0}
self.myprint("Generating list of files in bucket")
timer = datetime.now()
for key in self.bucket.list(prefix=self.prefix + remoteStorePath + "/"):
# self.myprint(key.name.rstrip().split('/')[-1])
self.s3_keys.append(key.name.rstrip().split('/')[-2] + key.name.rstrip().split('/')[-1])
self.myprint(str(len(self.s3_keys)) + " files in s3 bucket took " + str(datetime.now() - timer))
脚本中进一步向下的不同类:
def upload_file(self,details,ttl='259200'):
startTime = datetime.now()
initialHash = details[:2]
if initialHash == "zz":
initialHash = "ad"
local_file = os.path.join(overrideStorePath,"files",initialHash,details[2:])
k = boto.s3.key.Key(self.bucket)
k.key = self.prefix + remoteStorePath + '/' + details[:2] + '/' + details[2:]
k.set_metadata('Cache-Control','max-age=' + ttl)
k.set_contents_from_filename(local_file)
i = self.uploadTrack["i"] + 1
self.uploadTrack["i"] += 1
total = self.uploadTrack["total"]
p = int(100 * i / total)
self.myprint("Uploaded '{}' : {} of {} : {}% in {} : {} kb".format(k.name,str(i),str(total),str(p),str(datetime.now() - startTime),str(os.path.getsize(local_file) / 1024)))
return True
有更多文件以与脚本的第二个片段类似的方式上传,
对于这部分,我似乎无法在 boto3 中找到与“k = boto.s3.key.Key(self.bucket)
”等效的内容。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。