GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #2715

New issue
Copy link
New issue
Copy link
Closed
Closed
Can't download encrypted files larger than 1 MB from cloud storage#2715
Copy link
Assignees
daspecster
Labels
api: storageIssues related to the Cloud Storage API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Description

@dbanck
dbanck
opened on Nov 10, 2016
Issue body actions

I'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

  1. Upload a file larger than 1mb using a custom encryption key
  2. Try to download this file using the same encryption key
  3. 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.

Activity

  1. dbanck
    changed 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, 2016
  2. daspecster
    added
    api: storageIssues related to the Cloud Storage API.
    on Nov 10, 2016
  3. daspecster commented on Nov 10, 2016

    @daspecster
    daspecster
    on Nov 10, 2016
    Contributor
    More actions

    @dbanck thanks for reporting!

    @dbanck, I think that the encryption key shouldn't get passed to download_to_file() but rather is passed to the Blob constructor.

    See: https://github.com/GoogleCloudPlatform/google-cloud-python/blob/491519dc54cf2204703374ff374cabbd4cd14476/storage/google/cloud/storage/blob.py#L310-L311

  4. dbanck commented on Nov 10, 2016

    @dbanck
    dbanck
    on Nov 10, 2016
    Author
    More actions

    This might be true for the current master, but the docs have a different example: https://googlecloudplatform.github.io/google-cloud-python/stable/storage-blobs.html#google.cloud.storage.blob.Blob.download_to_file

    The files inside my local site-packages are looking like this: https://github.com/GoogleCloudPlatform/google-cloud-python/blob/umbrella-0.20.0/storage/google/cloud/storage/blob.py#L287

    Should I update to a newer version? But there is no newer version than 0.20.0 on pypi: https://pypi.python.org/pypi/google-cloud-storage/0.20.0

  5. daspecster commented on Nov 10, 2016

    @daspecster
    daspecster
    on Nov 10, 2016
    Contributor
    More actions

    @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.

  6. daspecster commented on Nov 11, 2016

    @daspecster
    daspecster
    on Nov 11, 2016
    Contributor
    More actions

    @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.

  7. dbanck commented on Nov 11, 2016

    @dbanck
    dbanck
    on Nov 11, 2016
    Author
    More actions

    Don't worry, take your time. Thanks for the feedback and looking into it :)

  8. daspecster
    added
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    on Nov 14, 2016
  9. daspecster
    added 3 commits that reference this issue on Nov 14, 2016

    Pass headers for streaming download. Fixes googleapis#2715

    This issue will close once commit 369034c is merged into the 'master' branch.
    369034c

    Pass headers for streaming download. Fixes googleapis#2715

    This issue will close once commit 14ce0b1 is merged into the 'master' branch.
    14ce0b1

    Pass headers for streaming download. Fixes googleapis#2715

    This issue will close once commit 0c77284 is merged into the 'master' branch.
    0c77284
  10. daspecster
    closed this as completedin #2730on Nov 14, 2016
  11. richkadel
    added a commit that references this issue on May 6, 2017

    Pass headers for streaming download. Fixes googleapis#2715

    This issue will close once commit aecef04 is merged into the 'master' branch.
    aecef04
  12. JustinBeckwith
    assigned
    daspecster
    on Feb 1, 2021
  13. parthea
    added a commit that references this issue on Nov 24, 2025

    Pass headers for streaming download. Fixes #2715

    This issue will close once commit 48ecfd1 is merged into the 'main' branch.
    48ecfd1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @daspecster
    daspecster

Labels

api: storageIssues related to the Cloud Storage API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.