I am trying to upload files from the browser to GCS.
I am using blobstore
API to upload files.
I went through the documentation and I could not find how to upload blob
to GCS.
How do I get the file from blobkey
so that I could upload it to GCS.
JSP side
<form action="<%= blobstoreService.createUploadUrl("/upload") %>"
method="post" enctype="multipart/form-data">
<input type="file" name="myFile">
<input type="submit" value="Submit">
</form>
servletSide
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobKey = blobs.get("myFile");
GcsService gcsService = GcsServiceFactory.createGcsService();
GcsFilename filename = new GcsFilename(BUCKETNAME, "exampleFile");
GcsFileOptions options = new GcsFileOptions.Builder().mimeType("text/plain")
.acl("authenticated-read")
.addUserMetadata("myfield1", "my field value")
.build();
gcsService.createOrReplace(filename, options, /*File from the blob key */);
Can any one help me how to save the files into GCS?
after going through the java docs i was able to figure out how to upload blob and multiple blob file to gcs.
<form action="<%= blobstoreService.createUploadUrl("/upload") %>"
method="post" enctype="multipart/form-data">
<input type="file" name="myFile">
<input type="file" name="myFile2">
<input type="file" name="myFile3">
<input type="submit" value="Submit">
</form>
servlet side
instead of getting upload blobkey info
Map<String, List<BlobKey>> blobkeylist = blobstoreService.getUploads(request);
i got blobInfos
Map<String, List<BlobInfo>> blobsData = blobstoreService.getBlobInfos(request);
for (String key : blobsData.keySet())
{
for(BlobInfo blob:blobsData.get(key))
{
byte[] b = new byte[(int)blob.getSize()];
BlobstoreInputStream in = new BlobstoreInputStream(blob.getBlobKey());
in.read(b);
GcsService gcsService = GcsServiceFactory.createGcsService();
GcsFilename filename = new GcsFilename(BUCKETNAME, "/testFolder3/"+blob.getFilename());
GcsFileOptions options = new GcsFileOptions.Builder()
.mimeType(blob.getContentType())
.acl("authenticated-read")
.addUserMetadata("myfield1", "my field value")
.build();
gcsService.createOrReplace(filename, options,ByteBuffer.wrap(b));
in.close();
}
}
this one worked. im not sure if it is the best solution, but its working.