I am using Backblaze B2 and b2sdk.v2 in Flask to upload files.
This is code I tried, using the upload
method:
# I am not showing authorization code...
def upload_file(file):
bucket = b2_api.get_bucket_by_name(bucket_name)
file = request.files['file']
bucket.upload(
upload_source=file,
file_name=file.filename,
)
This shows an error like this
AttributeError: 'SpooledTemporaryFile' object has no attribute 'get_content_length'
I think it's because I am using a FileStorage
instance for the upload_source
parameter.
I want to know whether I am using the API correctly or, if not, how should I use this?
Thanks
You're correct - you can't use a Flask
FileStorage
instance as a B2 SDKUploadSource
. What you need to do is to use theupload_bytes
method with the file's content:Note that this reads the entire file into memory. The
upload_bytes
method may need to restart the upload if something goes wrong (with the network, usually), so the file can't really be streamed straight through into B2.If you anticipate that your files will not fit into memory, you should look at using
create_file_stream
to upload the file in chunks.