How to serve cloudstorage files using the SDK application

In the application, I can use cloudstorage files such as pdf using the default container for my application:

http://storage.googleapis.com/<appid>.appspot.com/<file_name> 

But how can I serve local cloud storage files in the SDK without using blob_key?

I write to the default cart:

 gcs_file_name = '/%s/%s' % (app_identity.get_default_gcs_bucket_name(), file_name) with gcs.open(gcs_file_name, 'w') as f: f.write(data) 

Default basket name in SDK = 'app_default_bucket'

In the SDK repository, I have a view: GsFileInfo showing: filename: /app_default_bucket/example.pdf

Update and workaround: you can get the service URL for NON image files such as css, js and pdf.

 gs_file = '/gs/%s/%s/%s' % (app_identity.get_default_gcs_bucket_name(), folder, filename) serving_url = images.get_serving_url(blobstore.create_gs_key(gs_file)) 
+8
python google-app-engine google-cloud-storage sdk
source share
3 answers

UPDATE I found this function to work with cloudstorage files using the SDK:

This feature has not yet been documented .

 http://localhost:8080/_ah/gcs/app_default_bucket/filename 

In this case, we do not need the img URL to serve NON images, as shown below.

To create an e-service url for cloudstorage files such as images, css, js and pdf in default_bucket, I use this code for testing (SDK) and GAE production:

IMPORTANT: images.get_serving_url () also works for NON images in the SDK!

In the SDK, you'll need blobstore to read blob and create a serving URL for the cloudstorage object.

I also added code to read, write, and download blobs cloudstorage in the SDK and GAE products.

The code can be found here .

+8
source share

This is the value you see in development mode from app_identity_stub.py:

 APP_DEFAULT_GCS_BUCKET_NAME = 'app_default_bucket' 

The comments in this file explain this:

This service behaves the same as a production service, except for using constant values ​​instead of application-specific values.

You should get the correct URL in your production code.

EDIT:

This is from the support forum:

In design mode, application engine tools mimic Google Cloud Storage Services locally. The objects in this simulated environment are volatile, so your application fails because the desired object does not exist in the local store. If you first create (and optionally write to) the object you are trying to read, it should work well in dev mode (it did for me). Of course, the objects in the production service are permanent, so there is no need for this additional step when starting the application in production mode (if the object already exists).

Hope this helps,

Google Cloud Storage Team

This means that you must first write the file, then you can use it. If I understand correctly, you can use any bucket name for this purpose, including "app_default_bucket".

+4
source share

I was here before looking for answers and just wanted to share what I found, now that I am working.

You can do it now, and it only hurts a little. Image crawl or blobstore API is not supported and does not seem to work anymore.

Cm:

If you sign your URLs, you can specify automatically expiring links to your content for anonymous or paid use. You would not want to serve your entire site in this way, but for PDF or something else, this is a valid and semi-secure option.

Missing documentation, you may have to discard a new line for canonical extended headers. The storage endpoint will tell you what it expects when the signature is bad.

In addition, your host must be: https://storage-download.googleapis.com/

If you use App Engine, then GoogleAccessId : <projectname> @ appspot.gserviceaccount.com

See: app_identity.get_service_account_name()

An example of how to generate a signature:

 from google.appengine.api import app_identity def signFile(path, verb='GET', md5='', contentType='', expiration=''): signatureRequest = '{}\n{}\n{}\n{}\n{}'.format( verb, md5, contentType, expiration, path) return app_identity.sign_blob(signatureRequest) 

Returns a tuple (privateKey, binarySignature) .

Now you need to create the url. The signature must be encoded in base64 and then encoded. The following describes how to complete the construction of the URL. You should probably use the download node mentioned above.

Example URL from documents:

 https: //storage.googleapis.
 com/example-bucket/cat.jpeg?GoogleAccessId=example@example-project.iam.gservicea
 ccount.com & Expires = 1458238630 & Signature = VVUgfqviDCov% 2B% 2BKnmVOkwBR2olSbId51kSib
 uQeiH8ucGFyOfAVbH5J% 2B5V0gDYIioO2dDGH9Fsj6YdwxWv65HE71VEOEsVPuS8CVb% 2BVeeIzmEe8z
 7X7o1d% 2BcWbPEo4exILQbj3ROM3T2OrkNBU9sbHq0mLbDMhiiQZ3xCaiCQdsrMEdYVvAFggPuPq% 2FE
 QyQZmyJK3ty% 2Bmr7kAFW16I9pD11jfBSD1XXjKTJzgd% 2FMGSde4Va4J1RtHoX7r5i7YR7Mvf% 2Fb17
 zlAuGlzVUf% 2FzmhLPqtfKinVrcqdlmamMcmLoW8eLG% 2B1yYW% 2F7tlS2hvqSfCW8eMUUjiHiSWgZLE
 VIG4Lw% 3D% 3D

Hope this helps someone!

Oh yes, you only need to make all the signatures if your bucket is not publicly available (readable).

0
source share

All Articles