Reputation: 1931
I am using the FileService Api to save files to the Blobstore from server side like this:
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = null;
file = fileService.createNewBlobFile(contentType, fileName);
boolean lock = true;
FileWriteChannel writeChannel = null;
writeChannel = fileService.openWriteChannel(file, lock);
ByteBuffer bb = ByteBuffer.wrap(FILE_BYTES);
writeChannel.write(bb);
writeChannel.closeFinally();
BlobKey blobKey = fileService.getBlobKey(file);
return blobKey.getKeyString();
But the FileService Api is going out by July this year (2015) File Api Service Turndown.
I am searching for an alternative to do this. I need to create a file from a byte[] in server side and save it to the blobstore.
I know and use the createUploadUrl option to upload and save files to the blobstore from client side
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
url = blobstoreService.createUploadUrl("/upload", uploadOptions);
But what I need is to create and save files right in the server side, as the FileService Api do/did.
I have searched the migration documents for JAVA and there is no alternative for this. The only option is via POST with the createUploadUrl
Any help will be appreciated. Thanks for reading.
Upvotes: 0
Views: 372
Reputation: 3113
Since the Blobstore is near to the permanent shutdown, you need to starts using Google Cloud Storage.
The Google Cloud Storage Client Library is fully integrated for App Engine, making easy the migration to another API.
After you saved an object to Cloud Storage, you can generated a BlobKey starting from the object name.
GcsFilename gcs_file = /* your uploaded file */;
String cloudStorageURL = "/gs/" + gcs_file.getBucketName() + "/" + gcs_file.getObjectName();
BlobstoreService bs = BlobstoreServiceFactory.getBlobstoreService();
return bs.createGsBlobKey(cloudStorageURL);
In this way you don't need to alter your entire code (and possibly the datastore) because you keep using a BlobKey object as a file identifier.
Upvotes: 1