boto key.set_contents_from_string()失败了403(但是aws s3 cp有效)

时间:2015-01-06 01:09:14

标签: amazon-s3 boto

当我尝试使用boto上传文件时,它会以403 Forbidden失败,我想知道我错过了什么。

命令行aws s3 cp foobar s3://bucket-name/topdir/foobar运行正常,文件确实上传。

WITHIN boto,我可以列出存储桶并检索文件内容。

IAM用户perms to bucket设置为Put / Get / DeleteObject。我也尝试了其他方法,比如set_contents_from_file,但它们都失败了。 Python 2.6.8。

我想解决的问题是:从ftp读取文件并推送到S3。像:

k = boto.s3.key.Key(b)
k.name = 'new-name'
ftp_handle.retrbinary('remote-name', k.set_contents_from_string)

下面是我的python会话。非常感谢您的帮助。

$ python
>>> import boto
>>> conn = boto.connect_s3()
>>> b = conn.get_bucket("bucket-name")
>>> k = boto.s3.key.Key(b)
>>> k.name = "topdir/foobar"
>>> k.set_contents_from_string("hello, world")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib64/python2.6/site-packages/boto-2.32.1-py2.6.egg/boto/s3/key.py", line 1419, in set_contents_from_string
    encrypt_key=encrypt_key)
  File "/usr/local/lib64/python2.6/site-packages/boto-2.32.1-py2.6.egg/boto/s3/key.py", line 1286, in set_contents_from_file
    chunked_transfer=chunked_transfer, size=size)
  File "/usr/local/lib64/python2.6/site-packages/boto-2.32.1-py2.6.egg/boto/s3/key.py", line 746, in send_file
    chunked_transfer=chunked_transfer, size=size)
  File "/usr/local/lib64/python2.6/site-packages/boto-2.32.1-py2.6.egg/boto/s3/key.py", line 944, in _send_file_internal
    query_args=query_args
  File "/usr/local/lib64/python2.6/site-packages/boto-2.32.1-py2.6.egg/boto/s3/connection.py", line 664, in make_request
    retry_handler=retry_handler
  File "/usr/local/lib64/python2.6/site-packages/boto-2.32.1-py2.6.egg/boto/connection.py", line 1053, in make_request
    retry_handler=retry_handler)
  File "/usr/local/lib64/python2.6/site-packages/boto-2.32.1-py2.6.egg/boto/connection.py", line 923, in _mexe
    request.body, request.headers)
  File "/usr/local/lib64/python2.6/site-packages/boto-2.32.1-py2.6.egg/boto/s3/key.py", line 880, in sender
    response.status, response.reason, body)
boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>...</RequestId><HostId>...</HostId></Error>

我所关注的手册与我的boto版本略有不同步(2.32对2.34):http://boto.readthedocs.org/en/latest/ref/s3.html所以也许boto 2.32中不支持这个。但那应该不会产生403,不是吗?

1 个答案:

答案 0 :(得分:0)

您可能希望使用connect_to_region而不是s3connect。将文件上传到s3时遇到了类似的问题。