[英]upload files to google cloud storage using Blobstore API
I am trying to upload files from the browser to GCS. 我正在尝试将文件从浏览器上传到GCS。 I am using
blobstore
API to upload files. 我正在使用
blobstore
API上传文件。
I went through the documentation and I could not find how to upload blob
to GCS. 我浏览了文档,但是找不到如何将
blob
上传到GCS。
How do I get the file from blobkey
so that I could upload it to GCS. 如何从
blobkey
获取文件,以便我可以将其上传到GCS。
JSP side JSP方面
<form action="<%= blobstoreService.createUploadUrl("/upload") %>"
method="post" enctype="multipart/form-data">
<input type="file" name="myFile">
<input type="submit" value="Submit">
</form>
servletSide 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? 任何人都可以帮我如何将文件保存到GCS中吗?
after going through the java docs i was able to figure out how to upload blob and multiple blob file to gcs. 通过java文档后,我能够弄清楚如何将blob和多个blob文件上传到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 servlet方面
instead of getting upload blobkey info 而不是获取上传blobkey信息
Map<String, List<BlobKey>> blobkeylist = blobstoreService.getUploads(request);
i got blobInfos 我得到了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. 当您看到blobkey时,该文件已上载到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)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.