0
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));
    

But when i try to test this through postman i am getting 415 unsupported media type error.

Can any one help me with this.
Thanks in advance
Anu Priya
  • 33
  • 1
  • 7

1 Answers1

0

The recommended way to use Google Cloud Storage from Java is to use the Cloud Storage Client Libraries.

Below sample code 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;

// 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));

Refer this SO Link for more information on file upload using Java.

for 415 unsupported media type errors : refer to these Link1, Link2 which helps you in resolving this concern.

Hemanth Kumar
  • 2,728
  • 1
  • 4
  • 19