当我对S3存储桶的列表文件夹没有权限时,使用boto将文件上传到S3子文件夹

时间:2015-09-01 12:18:50

标签: python amazon-s3 boto

我的存储桶“basebucket”中有一个子文件夹“my / places / photos”。我已阅读此子文件夹的写入和删除权限。但是我对basebucket没有任何权限。

是否有使用Python boto我可以将多部分上传文件发送到“my / places / photos”。

我编写了以下代码但没有工作。桶对象本身没有被创建。给我一个例外。如果我尝试查找,我将获取桶对象为无。

def file_multipart_upload():
    conn = S3Connection(ACCESS_KEY, SECRET_KEY)
    bucket = conn.get_bucket(BUCKET_NAME)

    # Get file info
    file_to_upload = FILE_TO_UPLOAD
    source_size = os.stat(file_to_upload).st_size
    chunk_size = 5242880

    # Create a multipart upload request
    mp = bucket.initiate_multipart_upload(os.path.join(BUCKET_FOLDER_PATH, file_to_upload))
    chunk_count = int(math.ceil(source_size / float(chunk_size)))
    print("Chunk size is {0} for the file {1}".format(chunk_count, file_to_upload))

    # Send the file parts, using FileChunkIO to create a file-like object
    # that points to a certain byte range within the original file. We
    # set bytes to never exceed the original file size.
    for i in range(chunk_count):
        offset = chunk_size * i
        bytes = min(chunk_size, source_size - offset)
        with FileChunkIO(file_to_upload, 'r', offset=offset, bytes=bytes) as fp:
            mp.upload_part_from_file(fp, part_num=i + 1)
        print("Offset, bytes and part_num values of file {0} are {1}, {2} and {3}".format(file_to_upload, offset,
                                                                                          bytes, i + 1))
    # Finish the upload
    mp.complete_upload()

1 个答案:

答案 0 :(得分:1)

默认情况下,boto get_bucket方法调用尝试通过对存储桶网址执行HEAD请求来验证存在该存储桶。这将要求您有权列出存储桶。

如果您想避免此验证步骤,只需执行以下操作:

bucket = conn.get_bucket(BUCKET_NAME, validate=False)

和boto将跳过验证步骤。