Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python S3ResponseError:400在使用boto的多部分上传过程中的错误请求_Python_Amazon Web Services_Amazon S3_Boto - Fatal编程技术网

Python S3ResponseError:400在使用boto的多部分上传过程中的错误请求

Python S3ResponseError:400在使用boto的多部分上传过程中的错误请求,python,amazon-web-services,amazon-s3,boto,Python,Amazon Web Services,Amazon S3,Boto,我正试图使用boto上传一个大文件使用多部分上传 这是我的密码 import math, os import boto.s3.connection import boto from filechunkio import FileChunkIO # Connect to S3 c = boto.connect_s3() b = c.get_bucket('bucketA') # Get file info source_path = './A/tmp1/' source_size = os.s

我正试图使用boto上传一个大文件使用多部分上传

这是我的密码

import math, os
import boto.s3.connection
import boto
from filechunkio import FileChunkIO

# Connect to S3
c = boto.connect_s3()
b = c.get_bucket('bucketA')

# Get file info
source_path = './A/tmp1/'
source_size = os.stat(source_path).st_size

# Create a multipart upload request
mp = b.initiate_multipart_upload(os.path.basename(source_path))

# Use a chunk size of 50 MiB (feel free to change this)
chunk_size = 52428800
chunk_count = int(math.ceil(source_size / float(chunk_size)))

# 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(source_path, 'r', offset=offset, bytes=bytes) as fp:
        mp.upload_part_from_file(fp, part_num=i + 1)

# Finish the upload
mp.complete_upload()
我的错误堆栈是这样的

---------------------------------------------------------------------------
S3ResponseError                           Traceback (most recent call last)
<ipython-input-9-2cf74dccfef2> in <module>
     22 
     23 # Create a multipart upload request
---> 24 mp = b.initiate_multipart_upload(os.path.basename(source_path))
     25 
     26 # Use a chunk size of 50 MiB (feel free to change this)

/usr/local/lib/python3.7/site-packages/boto/s3/bucket.py in initiate_multipart_upload(self, key_name, headers, reduced_redundancy, metadata, encrypt_key, policy)
   1766         else:
   1767             raise self.connection.provider.storage_response_error(
-> 1768                 response.status, response.reason, body)
   1769 
   1770     def complete_multipart_upload(self, key_name, upload_id,

S3ResponseError: S3ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?><Error><Code>InvalidArgument</Code><Message>Request Content-Type is not multipart/form-data</Message></Error>
我到底需要在哪里设置请求内容类型? 由于某些依赖性限制,我正在使用boto,无法升级到boto3

有指针吗?

第一个参数是
键名
,即文件在存储桶中可用时的名称

看起来您传入的是本地目录的名称
。/a/tmp1/'
,而不是一个大文件。我不知道这是否被允许。键名上的S3文档不是非常清晰


另一个可能有用的部分是,
initiate\u multipart\u upload
函数将
headers
字典作为参数。这些是标准的http头,而
内容类型
头是标准的http头:。

虽然这肯定有帮助,但文档中不清楚如何添加头。这就是我使用的
mp=b.initiate\u multipart\u upload(os.path.basename(source\u path),headers={'Content-Type':“multipart/form data”}
但是,引发的错误是缺少多部分边界规范。然后,我将标题修改为
headers={'Content-Type':“multipart/form data;boundary=-”}
但是运气不好,我收到了boundary not found错误。我仍然不能100%确定您可以上传整个目录。你想上传每个文件吗?