gpt4 book ai didi

javascript - 如何将 InMemoryUploadedFile 上传到我的 S3 存储桶?

转载 作者:太空宇宙 更新时间:2023-11-03 15:47:15 25 4
gpt4 key购买 nike

我有一个简单的上传表单,其中包含一个图像 FileField :

def post(request):    
if request.user.is_authenticated():
form_post = PostForm(request.POST or None, request.FILES or None)
if form_post.is_valid():
instance = form_post.save(commit=False)
instance.user = request.user

instance.save()

return HttpResponseRedirect('/home/')
else:
form_post = PostForm()

context = {
'form_post': form_post,
}
return render(request, 'post/post.html', context)
else:
return HttpResponseRedirect("/accounts/signup/")

当用户向表单添加图像时,它会触发此 JS 函数:

$('input#id_image').on('change', function(e) {...} 

提供图像的预览。这是我希望将图像上传到我的媒体文件夹目录的地方(我正在使用 S3 存储)。默认情况下,当用户提交表单时会上传图片,但我希望它尽快上传 $('input#id_image').on('change'被触发。

到目前为止,我所做的是检索 InMemoryUploadedFile 中的图像我认为的格式:

$('input#id_image').on('change', function(e) {
var formData = new FormData();

formData.append('image', $('input[type=file]')[0].files[0]);
formData.append('csrfmiddlewaretoken', $("input[name='csrfmiddlewaretoken']").val());

$.ajax({
url: '/upload_image/',
data: formData,
type: 'POST',
contentType: false,
processData: false,
...
});

^ 发送到 View :

def upload_image(request):
if request.is_ajax():
img = request.FILES.get('image')
print(img) #successfully prints the filename

但在上传此 InMemoryUploadedFile 方面,我不知道下一步该怎么做到我的 S3 存储桶。有什么建议吗?

编辑

img = request.FILES.get('image')
print(img) # successfully prints the filename

s3_connection = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, host="us-east-1")
bucket = s3_connection.get_bucket('my-bucket', validate=False)
key = Key(bucket, img.name)
key.send_file(img) #this line fires the error
return HttpResponse('upload ok')

key.send_file(img)返回此错误:

socket.gaierror: [Errno 8] nodename nor servname provided, or not known

Traceback (most recent call last):
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/zorgan/Desktop/project/site/draft1/views.py", line 201, in upload_image
key.send_file(img)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/boto/s3/key.py", line 762, in send_file
chunked_transfer=chunked_transfer, size=size)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/boto/s3/key.py", line 963, in _send_file_internal
query_args=query_args
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/boto/s3/connection.py", line 671, in make_request
retry_handler=retry_handler
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/boto/connection.py", line 1071, in make_request
retry_handler=retry_handler)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/boto/connection.py", line 1030, in _mexe
raise ex
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/boto/connection.py", line 940, in _mexe
request.body, request.headers)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/boto/s3/key.py", line 803, in sender
http_conn.endheaders()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1102, in endheaders
self._send_output(message_body)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 934, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 877, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1252, in connect
super().connect()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 849, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 693, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 732, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

最佳答案

boto 工作时遇到问题,但我升级到 boto3 并且现在工作正常。

代码如下:

img = request.FILES.get('image')
session = boto3.Session(
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
)
s3 = session.resource('s3')

s3.Bucket('my-bucket').put_object(Key='media/%s' % img.name, Body=img)
return HttpResponse()

这会在 AJAX 调用期间成功将图像上传到我的存储桶。

关于javascript - 如何将 InMemoryUploadedFile 上传到我的 S3 存储桶?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49332470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com