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.
When you see a blobkey, the file is already uploaded to GCS. You can resave it with different options, if you want, but you don't have to upload it again - it's already in your bucket.
请参阅https://developers.google.com/appengine/docs/java/blobstore/#Java_Using_the_Blobstore_API_with_Google_Cloud_Storage (您需要提供createUploadUrl以及将googleStorageBucketName设置为目标存储区的UploadOptions)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.