Можно ли вернуть открытый поток записи из функции?
Я получаю сообщение об ошибке:
ValueError: операция ввода-вывода в закрытом файле.
import boto3
import io
s3 = boto3.client('s3')
# Download file from S3 and upload a copy
def download_s3():
response = s3.get_object(
Bucket="bedrock-data-files",
Key="test/filename.txt"
)
stream = io.BytesIO(response['Body'].read())
return stream
def upload_s3():
returnedStream = io.BytesIO()
s3.upload_fileobj(
returnedStream,
"bedrock-data-files",
"test/copy.txt"
)
return returnedStream
downloadstream = download_s3()
uploadstream = upload_s3()
for chunk in downloadstream.read():
uploadstream.write(chunk)
Эта версия работает, но мне нужно, чтобы поток записи создавался в функции.
def download_s3():
response = s3.get_object(
Bucket="bedrock-data-files",
Key="test/filename.txt"
)
stream = io.BytesIO(response['Body'].read())
return stream
downloadstream = download_s3()
print(downloadstream) # This line returns
s3.upload_fileobj(
downloadstream,
"bedrock-data-files",
"test/copy.txt"
)
Подробнее здесь: https://stackoverflow.com/questions/788 ... m-function