如何使用java将文件上传到GCS bucket


I am new to GCP and I want to upload a pem file to a particular bucket using java sdk. I followed the below code.
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));

但当我试图通过邮递员测试这一点时,我得到了415个不支持的媒体类型错误。

Can any one help me with this.
Thanks in advance

从Java使用Google云存储的推荐方法是使用云存储客户端库。

以下示例代码是如何使用客户端库将对象上传到Google云存储的示例:

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;
// You need to Create your service object : 
Storage storage = StorageOptions.getDefaultInstance().getService();
// You need 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上传文件的更多信息,请参阅此SO链接。

对于415个不受支持的媒体类型错误:请参阅这些Link1、Link2,它可以帮助您解决此问题。

相关内容

  • 没有找到相关文章

最新更新