Python S3 upload of zip file gives AttributeError: 'ZipFile' object has no attribute 'tell' error

Python S3 upload of zip file gives AttributeError: 'ZipFile' object has no attribute 'tell' error

I try to upload a zip file to my S3 bucket, but getting 

AttributeError: 'ZipFile' object has no attribute 'tell' error

conn = boto.s3.connect_to_region(region_name=s3region,
                                             aws_access_key_id=userid,
                                             aws_secret_access_key=accesskey,
                                      calling_format=boto.s3.connection.OrdinaryCallingFormat())

            bucket = conn.get_bucket(s3bucket)

            k = boto.s3.key.Key(bucket, zipf)

            k.send_file(zipf) //<----Gives Exception

这里有什么问题? (zipf 是我的压缩文件)

如果我这样修改代码;

   with open(zipf) as f:
            k.send_file(f)

我明白了

TypeError: coercing to Unicode: need string or buffer, ZipFile found

我创建了类似的 zip 文件;

zipfilename = 'AAA_' + str(datetime.datetime.utcnow().replace(microsecond=0)) + '.zip'
    zipf = zipfile.ZipFile(zipfilename, mode='w')

    for root, dirs, files in os.walk(path):
        for f in files:
            zipf.write(os.path.join(root, f))
    zipf.close()

您的 zip 文件是使用 zipf = zipfile.ZipFile(zipfilename, mode='w') 创建的。

zipfile documentation 之后,tell() 属性仅为 mode='r' 定义。

没有理由使用 zipfile 对象,而且在写入模式下,上传我们想要读取的文件(无论是 zip 还是其他),以便将其上传到 S3。

在键上调用 send_file() 之前只需使用 open(zipfilename, 'r')