我正在研究django 1.9上的一个项目并使用django-rest-framework。
Views.py:
@parser_classes((FileUploadParser,))
class upload_image(APIView):
def post(self, request, format=None):
file_obj = request.data['images']
upload_s3 = FileUpload()
folder_name = 'images'
file_url = upload_s3.put(file_obj, folder_name, file_obj.name)
class File_Upload:
def __init__(self):
self.env = {
"aws_access_key_id": "*************",
"aws_secret_access_key": "***********",
"region_name": "Asia Pacific (Singapore)"
}
self.bucketname = "my_bucket"
self.session = Session(**self.env)
self.s3_client = self.session.client('s3')
def put(self, bytes, folder, file_name):
self.s3_client.put_object(Body=bytes, Bucket=self.bucketname, Key=folder + "/" + file_name)
return "https://s3.%s.amazonaws.com/%s/%s/%s" % (self.env['region_name'], self.bucketname, folder, file_name)
在python shell上,我可以上传以及从亚马逊s3下载图像。我正在使用boto。
以下是我正在使用的代码:
>>> import boto
>>> import sys
>>> from boto.s3.connection import S3Connection
>>> from boto.s3.key import Key
>>> conn = boto.connect_s3("ACESS_KEY_ID","SECRET_ACCESS_KEY",host="s3-ap-southeast-1.amazonaws.com",is_secure = False)
>>> k = bucket.get_key("KEY_NAME")
>>> k.get_contents_to_filename("test.file")
>>> k.set_canned_acl('public-read')
>>> url = k.generate_url(0,query_auth=False,force_http=True)
我得到了一个工作网址。但问题来自于我想在views.py中执行相同操作(上面的代码),我收到此错误:
请告诉我问题出在哪里?S3ResponseError:400错误请求 请求方法:POST请求 网址:http://127.0.0.1:8000/admin/imagedata/image/add/ Django版本:1.9 异常类型:S3ResponseError 异常值:S3ResponseError:400 错误的请求 例外位置:/ Library /Python/2.7/site-packages/boto/s3/bucket.py in_get_key_internal,235行 Python可执行文件:/ usr / bin / python Python版本:2.7.10