Get Filepath from uploaded file on Google Cloud Storage - google-cloud-storage

I just configured my application to use Google Cloud Storage for storing user uploads. So far I was able to retrieve the file path of an uploaded by doing so:
ActiveStorage::Blob.service.send(:path_for, file.key)
However, on Google Cloud Storage this gives me the following error:
NoMethodError (undefined method `path_for' for #<ActiveStorage::Service::GCSService:...>)
How can I retrieve the filepath here?

You can use url_for_direct_upload(key, expires_in:, checksum:, **) or url(key, expires_in:, filename:, content_type:, disposition:) to get the GCS urls.
Reference

Related

Upload files in Drone Pipeline to Google Cloud Storage using gcs plugin

I have a drone cicd pipeline that builds a npm project which I want to upload to Google Cloud Storage (gcs). I found a drone gcs plugin which seems to be able to do so.
But I don't know what to use for the token parameter. The documentation says: "credentials to access Google Cloud Storage".
I have create a ServiceAccount and downloaded the json for it. My first attempt was to use the base64 encode json (as done with the App Engine Plugin) but this failed with this error:
failed to authenticate token: invalid character 'e' looking for beginning of value1
Is this a oauth2 token? How can I create a token, so that drone-ci can upload the files to my bucket?
I see the GCS plugin is broken :(, but I have added another plugin Google Cloud Auth that allows you to pass SA json as string secret and then use the auth plugin to activate the SA based auth.
You can then mount the ~/.config/gcloud in all the required steps and do the required gcloud tasks. For an example check https://plugins.drone.io/plugins/google-cloud-run that uses this method.
I hope this helps you.

Not able to access file in Google Cloud Storage using Cloud Vision API

I am currently trying to read a pdf file that I have stored in my Google Cloud Storage bucket. I have finished writing my code using the Google Cloud Vision API to read pdf files, but for some reason when I try to access the file that I have stored, I receive an error in my code saying that permission was denied to the file. How can I fix this, and if needed what code should I include to fix this?

Change default file/image if not found for public access in Google Cloud Storage

I migrated my website storage to google cloud storage, but the migration/upload was failed for some files because some reasons. By default, if the path/object doesnt exist in the GCS bucket will return XML like below:
<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Details>No such object: bucket/the_file.png</Details>
</Error>
Can I change/customize that return to another file/image?
If your static website is online by using a Cloud Storage bucket as is described in this document, you need to upload a public file in your bucket called 404.html, this file must have the html code that you desire to shown when an object doesn't exists.
After uploading the file 404.html it is mandatory to run this command in the cloud shell in order to define the error file
gsutil web set -e 404.html gs://www.example.com
In this page you can find more information about the 404.html file

Error Uploading CSV to Google Cloud Storage through Cloud SQL API

I have some data in Google Cloud SQL, which I am trying to transfer to Google Cloud Storage using Cloud SQL API(beta) (export) function. For this, I have installed jupyter notebook on google compute engine and I am running the python code (to connect to cloud sql API and export the date to cloud storage in a csv file) on that.
The python code does not throw any error and also there is no error body in API response. However, the API's response says "u'status': u'PENDING'" and I see the following error (in logs of Cloud SQL Dashboard) :
error uploading CSV file to GCS: gs://[BUCKET_NAME]/[FILE_NAME].csv: Access denied for account [SERVICE_ACCOUNT_NAME]#speckle-umbrella-11.iam.gserviceaccount.com (permission issue?)
How do I ensure I have all the relevant access for the account ([SERVICE_ACCOUNT_NAME]#speckle-umbrella-11.iam.gserviceaccount.com) ? I am unable to locate this account or give myself access to this .
By default Google Compute Engine instances have read only scope for Google Cloud Storage.
You should confirm that the Google Compute Engine instance scope is set to read/write.
You need to grant the Cloud SQL instance service account access to your bucket. Visit the export documentation and click the GCLOUD (2ND GEN) tab which will show you the commands you need to use.
The command to grant the service account access to the bucket is
gsutil acl ch -u [SERVICE_ACCOUNT_ADDRESS]:W gs://[BUCKET_NAME]

How to make IBM Bluemix Object Storage file publicly accessible?

On Bluemix, I created a Java application using Liberty for Java and the Object Storage Service. I then bound the Java Application and Object Storage Device. I uploaded the images into the container which I created in the Object Storage service. Now I want to access the uploaded images publicly, such as opening the images in a browser directly. I created the URL like the IBM Bluemix documentation said. After I access the URL in browser it shows the following error:
401 Unauthorized
Unauthorized
This server could not verify that you are authorized to access the document you requested.
My sample URL
Is it possible to make the URL public?
You can create a temporary public URL using the swift command line.
First you need to set a key and they create the temporary url. For example:
swift post -m "Temp-URL-Key:yourkey"
swift tempurl GET 3000 /v1/AUTH_90e12a182adf4a32bbd5e34645380244/offermsgs-cateimgs/books.jpg yourkey
The output of the command above will be your temporary public URL and in the example below it will be valid for 3000 seconds.
You can modify Object Storage ACL as well to make all files read only as suggested in the following post:
Public URLs For Objects In Bluemix Object Storage Service