Download GCS bucket blob with a question mark in it to file

google cloud storage download file python
google cloud storage download multiple files
google cloud storage read file python
download file from google cloud instance
google cloud storage download file java
blob download to filename
google cloud storage list files python
google cloud storage download folder python

I am encountering a weird error, that I suspect is caused by a question mark in my file. I have redacted the URIs a bit but you get the idea.

This works: gsutil cp gs://bucket-id/209146000/showphoto.aspx?photoid=12345.jpg test.jpg

But this:

client.download_blob_to_file('gs://bucket-id/209146000/showphoto.aspx?photoid=12345.jpg', open('test.jpg', 'wb'))

Gives me an error:

NotFound: 404 GET https://www.googleapis.com/download/storage/v1/b/marine-scrape/o/209146000%2Fshowphoto.aspx?alt=media: ('Request failed with status code', 404, 'Expected one of', <HTTPStatus.OK: 200>, <HTTPStatus.PARTIAL_CONTENT: 206>)

How do I avoid this? I probably should not have used the ? mark in the filename in the first place, but here we are. I have tried escaping it \? but it doesn't work. Any ideas?

My answer has two solutions.

Rename the object with the problem filename

  1. Login to the Google Cloud Console.
  2. Go to Storage.
  3. Select the bucket containing the file.
  4. Select the folder containing the object.
  5. On the far right side of the object, is the ellipse menu.
  6. Click this menu and select Rename.
  7. Rename the object.

Change the code

I reviewed the source code for the library google.cloud.storage version 1.20.0. The function download_blob_to_file() does not support URL encoding when the first parameter is a string. It does work for blob objects. I am including two different methods that support your filenames.

Method 1:

import  sys
import urllib.parse

# Imports the Google Cloud client library
from google.cloud import storage

bucket_name = 'bucket-id'
object_name = '209146000/showphoto.aspx?photoid=12345.jpg'
outfile = 'test.jpg'

client = storage.Client()

bucket = client.get_bucket(bucket_name)

blob = storage.Blob(object_name, bucket)

client.download_blob_to_file(blob, open('test.jpg', 'wb'))

Method 2:

import  sys
from google.cloud import storage

bucket_name = 'bucket-id'
object_name = '209146000/showphoto.aspx?photoid=12345.jpg'
outfile = 'test.jpg'

client = storage.Client()

bucket = client.get_bucket(bucket_name)

blob = bucket.blob(object_name)

blob.download_to_filename(outfile)

Error message when downloading blob to local file · Issue #5254 , Error message when downloading blob to local file #5254. Closed. lordcenzin What version of google-cloud-storage is installed? E.g.:. Download GCS bucket blob with a question mark in it to file I am encountering a weird error, that I suspect is caused by a question mark in my file. I have redacted the URIs a bit but you get the idea.

If you take a look on this Bucket and object naming guidelines you will find that you must avoid using special characters such "[", "]", "*", or "?" in your object names.

To avoid this just dont use special characters when naming your buckets or your objects.

From here you can just rename your files

GCS, I'm using a combination of the GCS python SDK and google API client to loop through a version-enabled bucket and download specific objects based on metadata. files from a complex directory tree (gs://bucketname/nfs/media/docs/​test1.txt) The /questions/48449299/gcs-python-download-blobs-with-directory-​structure. In this notebook, we'll be training T5 on a variant of this task which we call closed-book question answering. In closed-book QA, we feed the model a question without any context or access to

Those characters are permitted in GCS object names; it is gsutil that is interpreting them as wildcards, and thus preventing you from downloading the object.

Renaming so you don't use those characters would make it so gsutil doesn't have this problem for you, but if that's not possible you could use another tool to download the object (e.g., wget). There's a long-open bug against gsutil to support a raw mode to address this problem, but the bug is still open.

Downloading objects | Cloud Storage, Open the Cloud Storage browser. In the list of buckets, click on the name of the bucket that contains the object you want to download. The Bucket details page  Google Cloud Storage events can be used in any logical way. For example: With the Pipeline Finisher executor to stop the pipeline and transition the pipeline to a Finished state when the origin completes processing available data.

cp, Google Cloud Blog Similarly, you can download text files from a bucket by doing: "sliced" downloads in parallel when downloading large objects from Cloud Storage. is set small to avoid this problem, you can disable sliced object download if necessary by Java is a registered trademark of Oracle and/or its affiliates. How To Upload File To Google Cloud Storage Using Python

How to download files from Google Cloud Storage with Python and , Google Cloud Storage are used for a range of scenarios to store data including storing Open in app · Become a member · Sign in · Open in app. Get started First, specify the file that need to download and define the local folder location where blobs=bucket.list_blobs(prefix=file_id, delimiter=delimiter). Google Cloud Storage customers access their data through a web browser or command-line interface. Click on the Main Menu. We need to copy that file into a bucket in GCS. Checking file's existence by name in a GCS bucket with Node. Assuming that you already have accounts with Google Cloud and AWS and the access to read or write to both storage

The Download on Reading Data from Google Cloud Storage, like to do with it. When serving data to your users from your Google Cloud Storage… Click here to visit our frequently asked questions about HTML5 video. The CP command will upload or download files from your local machine to Google Cloud. For our “””Downloads a blob from the bucket.”””. Are you satisfied with our content and quality? How do you feel about Tricentis Tosca? Please help us improve & share your feedback!

Comments
  • Tried your second suggestion and using Python 3.7 unfortunately got the same error @Gerard did. But perhaps someone has a different result?
  • Update: same issue using Python 2.7.
  • @Maxim - I updated my answer with Python 3 code that I tested.
  • Thank you, have not tried the code, but I appreciate the work around. What I personally end up doing to rename them programatically is to use ipython to issue gsutil mv commands for each file with a question mark.
  • What do you mean by from here exactly?
  • From here = In your actual situation