GeezerVison is showing github.com — Can't download encrypted files larger than 1 MB from cloud storage · Issue #2715 · googleapis/google-cloud-python
Can't download encrypted files larger than 1 MB from cloud storage #2715Labels api: storageIssues related to the Cloud Storage API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns. DescriptionI'm using upload_from_file with encryption_key to store files inside a Google Cloud Storage bucket. This works very well.
Now I'm trying to download these files again. Using download_to_file with the same encryption_key. This crashes with the following stack trace:
Traceback (most recent call last):
File "restore.py", line 40, in <module>
main()
File "restore.py", line 37, in main
download_from_storage(args.file, args.bucket, args.credentials, key)
File "restore.py", line 17, in download_from_storage
blob.download_to_file(my_file, encryption_key=encryption_key)
File "/Users/daniel/Projects/python/gcs-backup/env/lib/python2.7/site-packages/google/cloud/storage/blob.py", line 354, in download_to_file
download.initialize_download(request, client._connection.http)
File "/Users/daniel/Projects/python/gcs-backup/env/lib/python2.7/site-packages/google/cloud/streaming/transfer.py", line 390, in initialize_download
self.stream_file(use_chunks=True)
File "/Users/daniel/Projects/python/gcs-backup/env/lib/python2.7/site-packages/google/cloud/streaming/transfer.py", line 614, in stream_file
response = self._process_response(response)
File "/Users/daniel/Projects/python/gcs-backup/env/lib/python2.7/site-packages/google/cloud/streaming/transfer.py", line 528, in _process_response
raise TransferRetryError(response.content)
google.cloud.streaming.exceptions.TransferRetryError: The target object is encrypted by a customer-supplied encryption key.
I'm using google-cloud-storage==0.20.0.
This only occurs when downloading files larger than 1mb. Any file less than 1mb is downloaded and decrypted as expected. A file larger than 1mb might be downloaded in chunks and only the first 1mb chunk is saved to disk.
Steps to reproduce
- Upload a file larger than 1mb using a custom encryption key
- Try to download this file using the same encryption key
- Find a 1mb chunk of your file at the destination
demo code
#!/usr/bin/env python
from tempfile import NamedTemporaryFile
import os
from google.cloud import storage
from google.cloud.storage import Blob
CREDENTIALS = './credentials.json'
BUCKET_NAME = '< SET BUCKET >'
ENCRYPTION_KEY = 'v3CtoFyEJmj52RGsSqze7C8fD6mzgpnd'
FILE_NAME = 'enc-test-file'
client = storage.Client.from_service_account_json(CREDENTIALS)
bucket = client.get_bucket(BUCKET_NAME)
def upload_to_storage(filename):
blob = Blob(FILE_NAME, bucket)
with open(filename, 'rb') as my_file:
blob.upload_from_file(my_file, encryption_key=ENCRYPTION_KEY)
def download_from_storage(filename):
blob = Blob(FILE_NAME, bucket)
with open(filename, 'wb') as my_file:
blob.download_to_file(my_file, encryption_key=ENCRYPTION_KEY)
def main():
size = 2097152 # 2mb file
f = NamedTemporaryFile(delete=False)
f.write("\0" * size)
f.close()
print('Uploading {} to Google Cloud Storage...'.format(f.name))
upload_to_storage(f.name)
print('Downloading {} from Google Cloud Storage...'.format(f.name))
download_from_storage(f.name)
os.remove(f.name)
if __name__ == '__main__':
main()
This might be because the encryption headers aren't added to the request for the next chunk. Activitychanged the title [-]Can't download encrypted files larger than 1 MB[/-] [+]Can't download encrypted files larger than 1 MB from cloud storage[/+] on Nov 10, 2016daspecster commented on Nov 10, 2016 dbanck commented on Nov 10, 2016 daspecster commented on Nov 10, 2016 @dbanck, thanks! I forgot that it changed since the last release.
This might be because the encryption headers aren't added to the request for the next chunk.
I'll take a look at this and see if I can track down the issue. daspecster commented on Nov 11, 2016 @dbanck sorry for the slow update on this. I've been playing with it and I thought it was completely fixed on master but it appears that it may not be. I'm confirming that and then I'll try and get a fix in. dbanck commented on Nov 11, 2016 Don't worry, take your time. Thanks for the feedback and looking into it :) added 3 commits that reference this issue on Nov 14, 2016This issue will close once commit 369034c is merged into the 'master' branch.369034c This issue will close once commit 14ce0b1 is merged into the 'master' branch.14ce0b1 This issue will close once commit 0c77284 is merged into the 'master' branch.0c77284 added a commit that references this issue on May 6, 2017This issue will close once commit aecef04 is merged into the 'master' branch.aecef04 added a commit that references this issue on Nov 24, 2025This issue will close once commit 48ecfd1 is merged into the 'main' branch.48ecfd1
MetadataMetadataapi: storageIssues related to the Cloud Storage API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns. DevelopmentNo branches or pull requestsIssue actions
|