首页 > 解决方案 > 无法使用 lambda 函数将视频上传到 s3

问题描述

我正在尝试通过 lambda 函数将不同格式的文件上传到 s3 存储桶,为此,我在本地系统中对文件进行编码,然后在 lambda 中解码文件并将其保存在 s3

Lambda 代码

import boto3
import base64

def lambda_handler(event, context):
    
    encoded_data=event["content"]
    
    decoded_string =base64.b64decode(encoded_data)
  
    
    file_name=event['file_name']

    bucket_name = "xxxx"
    lambda_path = "/tmp/" + file_name
    s3_path = "xxxx-data/xxx-videos/" + file_name

    s3 = boto3.resource("s3")
    s3.Bucket(bucket_name).put_object(Key=s3_path, Body=decoded_string)

调用 lambda 的本地系统代码

import base64
import requests
import json

with open("21.mp4", "rb") as video_file:
    encoded_string = base64.b64encode(video_file.read())

text= encoded_string.decode('utf-8')
    
body = {"content":text,"file_name":"21.mp4"}

headers= { "Access-Control-Allow-Origin": "*", "Content-Type": "application/json" }

r1 = requests.post(aws_lambda_url, data=json.dumps(body),headers=headers)

我可以上传 pdf、图像文件,但在上传视频文件时出现以下错误,如何解决此错误?

SysCallError                              Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py in _send_until_done(self, data)
    316             try:
--> 317                 return self.connection.send(data)
    318             except OpenSSL.SSL.WantWriteError:

~/anaconda3/lib/python3.7/site-packages/OpenSSL/SSL.py in send(self, buf, flags)
   1728         result = _lib.SSL_write(self._ssl, buf, len(buf))
-> 1729         self._raise_ssl_error(self._ssl, result)
   1730         return result

~/anaconda3/lib/python3.7/site-packages/OpenSSL/SSL.py in _raise_ssl_error(self, ssl, result)
   1630                     if errno != 0:
-> 1631                         raise SysCallError(errno, errorcode.get(errno))
   1632                 raise SysCallError(-1, "Unexpected EOF")

SysCallError: (32, 'EPIPE')

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    599                                                   body=body, headers=headers,
--> 600                                                   chunked=chunked)
    601 

~/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    353         else:
--> 354             conn.request(method, url, **httplib_request_kw)
    355 

~/anaconda3/lib/python3.7/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1228         """Send a complete request to the server."""
-> 1229         self._send_request(method, url, body, headers, encode_chunked)
   1230 

~/anaconda3/lib/python3.7/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1274             body = _encode(body, 'body')
-> 1275         self.endheaders(body, encode_chunked=encode_chunked)
   1276 

~/anaconda3/lib/python3.7/http/client.py in endheaders(self, message_body, encode_chunked)
   1223             raise CannotSendHeader()
-> 1224         self._send_output(message_body, encode_chunked=encode_chunked)
   1225 

~/anaconda3/lib/python3.7/http/client.py in _send_output(self, message_body, encode_chunked)
   1054                         + b'\r\n'
-> 1055                 self.send(chunk)
   1056 

~/anaconda3/lib/python3.7/http/client.py in send(self, data)
    976         try:
--> 977             self.sock.sendall(data)
    978         except TypeError:

~/anaconda3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py in sendall(self, data)
    327         while total_sent < len(data):
--> 328             sent = self._send_until_done(data[total_sent:total_sent + SSL_WRITE_BLOCKSIZE])
    329             total_sent += sent

~/anaconda3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py in _send_until_done(self, data)
    322             except OpenSSL.SSL.SysCallError as e:
--> 323                 raise SocketError(str(e))
    324 

OSError: (32, 'EPIPE')

During handling of the above exception, another exception occurred:

ProtocolError                             Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    448                     retries=self.max_retries,
--> 449                     timeout=timeout
    450                 )

~/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    637             retries = retries.increment(method, url, error=e, _pool=self,
--> 638                                         _stacktrace=sys.exc_info()[2])
    639             retries.sleep()

~/anaconda3/lib/python3.7/site-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
    366             if read is False or not self._is_method_retryable(method):
--> 367                 raise six.reraise(type(error), error, _stacktrace)
    368             elif read is not None:

~/anaconda3/lib/python3.7/site-packages/urllib3/packages/six.py in reraise(tp, value, tb)
    684         if value.__traceback__ is not tb:
--> 685             raise value.with_traceback(tb)
    686         raise value

~/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    599                                                   body=body, headers=headers,
--> 600                                                   chunked=chunked)
    601 

~/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    353         else:
--> 354             conn.request(method, url, **httplib_request_kw)
    355 

~/anaconda3/lib/python3.7/http/client.py in request(self, method, url, body, headers, encode_chunked)
   1228         """Send a complete request to the server."""
-> 1229         self._send_request(method, url, body, headers, encode_chunked)
   1230 

~/anaconda3/lib/python3.7/http/client.py in _send_request(self, method, url, body, headers, encode_chunked)
   1274             body = _encode(body, 'body')
-> 1275         self.endheaders(body, encode_chunked=encode_chunked)
   1276 

~/anaconda3/lib/python3.7/http/client.py in endheaders(self, message_body, encode_chunked)
   1223             raise CannotSendHeader()
-> 1224         self._send_output(message_body, encode_chunked=encode_chunked)
   1225 

~/anaconda3/lib/python3.7/http/client.py in _send_output(self, message_body, encode_chunked)
   1054                         + b'\r\n'
-> 1055                 self.send(chunk)
   1056 

~/anaconda3/lib/python3.7/http/client.py in send(self, data)
    976         try:
--> 977             self.sock.sendall(data)
    978         except TypeError:

~/anaconda3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py in sendall(self, data)
    327         while total_sent < len(data):
--> 328             sent = self._send_until_done(data[total_sent:total_sent + SSL_WRITE_BLOCKSIZE])
    329             total_sent += sent

~/anaconda3/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py in _send_until_done(self, data)
    322             except OpenSSL.SSL.SysCallError as e:
--> 323                 raise SocketError(str(e))
    324 

ProtocolError: ('Connection aborted.', OSError("(32, 'EPIPE')"))

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
<ipython-input-5-b7df22d0e978> in <module>
     14 headers= { "Access-Control-Allow-Origin": "*", "Content-Type": "application/json" }
     15 
---> 16 r1 = requests.post(resume_store_api, data=json.dumps(body),headers=headers)
     17 
     18 print(r1.json())

~/anaconda3/lib/python3.7/site-packages/requests/api.py in post(url, data, json, **kwargs)
    114     """
    115 
--> 116     return request('post', url, data=data, json=json, **kwargs)
    117 
    118 

~/anaconda3/lib/python3.7/site-packages/requests/api.py in request(method, url, **kwargs)
     58     # cases, and look like a memory leak in others.
     59     with sessions.Session() as session:
---> 60         return session.request(method=method, url=url, **kwargs)
     61 
     62 

~/anaconda3/lib/python3.7/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    531         }
    532         send_kwargs.update(settings)
--> 533         resp = self.send(prep, **send_kwargs)
    534 
    535         return resp

~/anaconda3/lib/python3.7/site-packages/requests/sessions.py in send(self, request, **kwargs)
    644 
    645         # Send the request
--> 646         r = adapter.send(request, **kwargs)
    647 
    648         # Total elapsed time of the request (approximately)

~/anaconda3/lib/python3.7/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    496 
    497         except (ProtocolError, socket.error) as err:
--> 498             raise ConnectionError(err, request=request)
    499 
    500         except MaxRetryError as e:

ConnectionError: ('Connection aborted.', OSError("(32, 'EPIPE')"))

标签: pythonamazon-s3videoaws-lambdabase64

解决方案


推荐阅读