Image service serving url from AppEngine can be received using the following code.
ServingUrlOptions options = ServingUrlOptions.Builder
.withGoogleStorageFileName("/gs/" + bucket + "/image.jpeg")
.secureUrl(true);
String url = imagesService.getServingUrl(options);
How to get image serving Url through Google Cloud Java client library?
Github Link doesn't give much information about Image services. Is this been supported?
Please make sure your image is public. blob.getMediaLink() will print the image url.
StorageOptions options = StorageOptions.newBuilder().setProjectId(PROJECT_ID)
.setCredentials(GCP_CREDENTIALS).build();
Storage storage = options.getService();
Blob blob = storage.get(BUCKET_NAME, IMAGE_FILE_NAME);
String imageURL = blob.getMediaLink();
Related
I am trying to transform images using BlobstoreServiceFactory.
The code is running perfectly on Google cloud app engine. But when I am running the code from local tomcat sever it's throwing the below.
I have set the service account json file in system environment variable. I can read the bucket content from eclipse but while transforming getting the below exception. Please help..
for (StorageObject object : bucketContents) {
// Error coming from this line:
BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + bucket + "/" + object.getName());
Image blobImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
Transform resize1 = ImagesServiceFactory.makeResize(width, height);
Image resizeImage1 = imagesService.applyTransform(resize1, blobImage);
}
Exception Details
com.google.apphosting.api.ApiProxy$CallNotFoundException: Can't make API call blobstore.CreateEncodedGoogleStorageKey in a thread that is neither the original request thread nor a thread created by ThreadManager.
Hello all actually i am using google cloud platform and there i am storing my coupons images in gcs buckets. Now does google provide any api to delete an existing image from gcs buckets. I searched a lot on its docs google docs also have seen many blogs but what everyone do is deleting data from database but no one tell about how to delete image from buckets. If anyone have done this please help me it would be really appreciable.
Thanks
Sure.
Via console you can use gsutil command in this way. You just should install gsutil command.
Via api rest you can use this service. You can try this api here.
Also there are libraries for python, java or other languajes.
From #MikeSchwartz suggestion. Using cloud console, you can manage your objects manually. Link.
Update 2: example on NodeJS
We can choose between three options. Using request module, Google cloud NodeJS client or Google API NodeJS client. But first of all, you should authorise your server to make request to Google Cloud Storage (GCS). To do that:
Open the Console Credentials page
If it's not already selected, select the project that you're creating credentials for.
Click Create credentials​ and choose Service Account Key.
In the dropdown select Compute Engine default service account. Then click create. A Json file will be downloaded.
In the left panel click Overview and type cloud storage in the finder.
Click Google Cloud Storage and make sure that this api is enabled.
Rename the downloaded json to keyfile.json and put it in accesible path for your NodeJS code.
Google cloud NodeJS client. Here the official repository with a lot of samples.
var fs = require('fs');
var gcloud = require('gcloud');
var gcs = gcloud.storage({
projectId: 'your-project',
keyFilename: '/path/to/keyfile.json'
});
var bucket = gcs.bucket('your-bucket');
var file = bucket.file('your-file');
file.delete(function(err, apiResponse) {}):
Using request module.
npm install request
Then in your code:
var request = require('request');
request({
url: 'https://www.googleapis.com/storage/v1/b/your-bucket/o/your-file',
qs: {key: 'your-private-key'}, // you can find your private-key in your keyfile.json
method: 'DELETE'
}, function(error, response, body){});
Using Google API NodeJS: I don't know how to use this, but there a lot of examples here.
Assuming you have your image file public url, you can do it like this
import {Storage} from "#google-cloud/storage";
const storage = new Storage({
projectId: GCLOUD_PROJECT,
keyFilename: 'keyfile.json'
});
const bucket = storage.bucket(GCLOUD_BUCKET);
//var image_file="https://storage.googleapis.com/{bucketname}/parentfolder/childfolder/filename
var image_file="https://storage.googleapis.com/1533406597315/5be45c0b8c4ccd001b3567e9/1542186701528/depositphotos_173658708-stock-photo-hotel-room.jpg";
new Promise((resolve, reject) => {
var imageurl = image_file.split("/");
imageurl = imageurl.slice(4, imageurl.length + 1).join("/");
//imageurl=parentfolder/childfolder/filename
storage
.bucket(GCLOUD_BUCKET)
.file(imageurl)
.delete()
.then((image) => {
resolve(image)
})
.catch((e) => {
reject(e)
});
});
check on googles official documentation under code samples in this link https://cloud.google.com/storage/docs/deleting-objects or github https://github.com/googleapis/nodejs-storage/blob/master/samples/files.js
I am storing a file in Google App Engine using Google Cloud Storage. The file is loaded fine but the serve returns a file interpreted as a binary not as the original mime type. You'll find hereafter the code. Has anyone an idea of what is happening ?
I. Store file
GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()
.setBucket(BUCKET_NAME)
.setKey(objectId)
.setAcl("project-private")
.setMimeType(mimeType)
.setContentDisposition("attachment;filename "+item.getName());
II. Serve file
blobKey = blobstoreService.createGsBlobKey("/gs/"+BUCKET_NAME+"/"+ fileName);
blobstoreService.serve(blobKey, resp);
III. Difference with serving a file in the blobstore
I had a piece of code which used to work. The problem is that apparently the BlobInfo object is only for objects stored in the blobstore and not in the Google Cloud Storage
BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(blobKey);
resp.setContentLength(new Long(blobInfo.getSize()).intValue());
resp.setHeader("content-type", blobInfo.getContentType());
resp.setHeader("content-disposition", "attachment; filename=" +
blobInfo.getFilename());
blobstoreService.serve(blobKey, resp);
Any help is very welcome !
Thanks,
Hugues
Because you're serving the blob yourself, using the blob serving service, it's up to you to set all the HTTP headers, including the content type, correctly. If you want to use the content type of the stored object, you should fetch it and set it in the headers yourself.
Alternatively, you could link directly to the object's path in Google Storage, in which case it will be served by the Google Storage infrastructure, with the correct mimetype.
I am using App Engine to serve images based on blob keys from BlobStore. For example, this is how I get the URL for a stored image in Python:
class Photo(db.Model):
blob_key = blobstore.BlobReferenceProperty()
url = images.get_serving_url(self.blob_key)
My question is this: Does App Engine strip EXIF data from the images in the returned URL?
I'm trying to store audio files in google app engine's blobstore and play them in a browser. The problem I'm running into is that the data I'm getting in the browser is the actual mp3 data. I was expecting to get a url to play the mp3 in the blobstore. So, my question is, what do I need to change to get a url to play the blob instead of the audio data?
Here is my server side handler.
class ServeBlobHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self):
user = users.get_current_user()
query = db.GqlQuery("SELECT * FROM AudioData Where userId = :1", user.user_id())
results = query.fetch(limit=300)
for dStoreEntry in results:
entityBlobInfo = dStoreEntry.audioBlob
self.send_blob(entityBlobInfo)
This is the client side.
$.ajax({
url : '/serve_blob/audio/',
type : 'GET',
dataType : 'text',
success : function(data) {
alert('GET, audio data : \n '+ data );
}
});
The URL of the page that you're currently fetching the data from is the URL of the MP3. You'll need to use a web-based player of some sort to play it.
What Content-type header does your browser get for mp3 request? I'm guessing it's application/octet-stream
See what Blobstore docs say about upload:
If you don't specify a content type, the Blobstore will try to infer it from the
file extension. If no content type can be determined, the newly created blob is
assigned content type application/octet-stream
Go to GAE admin pages and check Blob Viewer to see under what content type was assigned to your mp3 files.
Get JPlayer - http://www.jplayer.org/
And then your example should work fine. We use it with appengine blobstore in java and it's great. The url from the blobstore will work in jplayer.
You can also set cache headers on your blob urls if you rewrite them to remove any query parameters and save yourself the costs of serving each stream.