[英]How to read pickle file from AWS S3 nested directory?
I have a pickle file in a nested directory inside AWS S3 Bucket, but I'm not able to load it with boto3
library to use it with AWS Lambda.我在 AWS S3 Bucket 的嵌套目录中有一个 pickle 文件,但我无法使用
boto3
库加载它以将其与 AWS Lambda 一起使用。
I've tried to follow the answers to this question , but no one works.我试图遵循这个问题的答案,但没有人工作。 This is my code so far:
到目前为止,这是我的代码:
s3 = boto3.resource('s3')
source_bucket = "source_bucket_name"
key = "folder1/pickle_file.p"
response = s3.Bucket(source_bucket).Object(key).get()
body_string = response['Body'].read()
try:
loaded_pickle = pickle.loads(body_string)
except Exception as e:
print(e)
When loading this function into AWS, I'm getting the following error message:将此 function 加载到 AWS 时,我收到以下错误消息:
embedded null byte
Your code seems fine apart from what I said in the comments.除了我在评论中所说的之外,您的代码似乎还不错。 Maybe your upload to S3 is incorrect then.
那么您上传到 S3 的内容可能不正确。 below is full working example:
以下是完整的工作示例:
import pickle
import boto3
mylist = [1,2,3]
# create pickle file
with open('/tmp/pickle_file.p', 'wb') as f:
pickle.dump(mylist, f)
# upload to s3
source_bucket='source_bucket_name'
key = "folder1/pickle_file.p"
with open('/tmp/pickle_file.p', 'rb') as f:
response = boto3.client('s3').put_object(
Body=f,
Bucket=source_bucket,
Key=key)
print(response)
# read back from s3
s3 = boto3.resource('s3')
response = s3.Bucket(source_bucket).Object(key).get()
body_string = response['Body'].read()
try:
loaded_pickle = pickle.loads(body_string)
except Exception as e:
print(e)
# should print out `mylist`
print(loaded_pickle)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.