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("[email protected]").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();
The recommended way to use Google Cloud Storage from Java is to use the Cloud Storage Client Libraries.
The GitHub page for this client gives several examples and resources to learn how to use it properly.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With