简体   繁体   English

使用Blobstore API将文件上传到Google云端存储

[英]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.

相关问题 使用blobstore api从云存储中检索文件的Google App引擎Java错误 - google app engine java error using blobstore api to retrieve file from cloud storage 如何使用Java API在一个调用中将多个文件上传到Google Cloud Storage? - How to upload multiple files to Google Cloud Storage in a single call using Java API? 使用Google API Explorer将数据上传到Google云存储 - using google api explorer to upload data to google cloud storage 如何在Google App Engine中替代Cloud Storage和BlobStore API处理Blob - How to handle Blobs in Google App Engine alternatively to Cloud Storage and BlobStore API Google App Engine和Java:将文件上传到Blobstore - Google App Engine & Java : upload files into the blobstore Google Cloud Storage使用Java上传文件 - Google Cloud Storage upload a file using java 从Google Cloud Storage Java API上传Firebase存储 - firebase storage upload from google cloud storage java api 无法使用GCS客户端库+ java将文件从GAE项目上传到Google云存储 - Not able to upload files from GAE project to Google cloud Storage using GCS Client library+java 如何使用Java API将超过32Mb的文件上传到谷歌云存储 - How to upload a file more that 32Mb to google cloud storage using Java API 通过块文件上传的Google Cloud Storage Java XML API块 - Google Cloud Storage Java XML API chunk by chunk file upload
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM