如何使用Java上传Google云端存储中的文件 [英] How to upload a file in the google cloud storage using java
问题描述
很长时间以来,我一直在尝试使用Java在Google云存储中上传文件.通过浏览,我找到了此代码,但无法完全理解.任何人都可以自定义此文件以在GCS中上传文件吗?
I have been trying for a long time to upload a file in the Google cloud store using java. By goggling I have found this code, but cant able to understand exactly. Can anyone please customize this one to upload a file in the GCS?
// Given
InputStream inputStream; // object data, e.g., FileInputStream
long byteCount; // size of input stream
InputStreamContent mediaContent = new InputStreamContent("application/octet-stream", inputStream);
// Knowing the stream length allows server-side optimization, and client-side progress
// reporting with a MediaHttpUploaderProgressListener.
mediaContent.setLength(byteCount);
StorageObject objectMetadata = null;
if (useCustomMetadata) {
// If you have custom settings for metadata on the object you want to set
// then you can allocate a StorageObject and set the values here. You can
// leave out setBucket(), since the bucket is in the insert command's
// parameters.
objectMetadata = new StorageObject()
.setName("myobject")
.setMetadata(ImmutableMap.of("key1", "value1", "key2", "value2"))
.setAcl(ImmutableList.of(
new ObjectAccessControl().setEntity("domain-example.com").setRole("READER"),
new ObjectAccessControl().setEntity("user-administrator@example.com").setRole("OWNER")
))
.setContentDisposition("attachment");
}
Storage.Objects.Insert insertObject = storage.objects().insert("mybucket", objectMetadata,
mediaContent);
if (!useCustomMetadata) {
// If you don't provide metadata, you will have specify the object
// name by parameter. You will probably also want to ensure that your
// default object ACLs (a bucket property) are set appropriately:
// https://developers.google.com/storage/docs/json_api/v1/buckets#defaultObjectAcl
insertObject.setName("myobject");
}
// For small files, you may wish to call setDirectUploadEnabled(true), to
// reduce the number of HTTP requests made to the server.
if (mediaContent.getLength() > 0 && mediaContent.getLength() <= 2 * 1000 * 1000 /* 2MB */) {
insertObject.getMediaHttpUploader().setDirectUploadEnabled(true);
}
insertObject.execute();
推荐答案
从Java使用Google Cloud Storage的推荐方法是使用
The recommended way to use Google Cloud Storage from Java is to use the Cloud Storage Client Libraries.
该客户端的 GitHub页面给出了一些示例和资源,以学习如何正确使用它.
The GitHub page for this client gives several examples and resources to learn how to use it properly.
它还提供了此代码示例,作为如何使用客户端库将对象上传到Google Cloud Storage的示例:
It also gives this code sample as an example of how to upload objects to Google Cloud Storage using the client library:
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
// Create your service object
Storage storage = StorageOptions.getDefaultInstance().getService();
// Create a bucket
String bucketName = "my_unique_bucket"; // Change this to something unique
Bucket bucket = storage.create(BucketInfo.of(bucketName));
// Upload a blob to the newly created bucket
BlobId blobId = BlobId.of(bucketName, "my_blob_name");
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
Blob blob = storage.create(blobInfo, "a simple blob".getBytes(UTF_8));
这篇关于如何使用Java上传Google云端存储中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!