use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class DownloadObject method downloadObject.
public static void downloadObject(String projectId, String bucketName, String objectName, String destFilePath) {
// The ID of your GCP project
// String projectId = "your-project-id";
// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";
// The ID of your GCS object
// String objectName = "your-object-name";
// The path to which the file should be downloaded
// String destFilePath = "/local/path/to/file.txt";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Blob blob = storage.get(BlobId.of(bucketName, objectName));
blob.downloadTo(Paths.get(destFilePath));
System.out.println("Downloaded object " + objectName + " from bucket name " + bucketName + " to " + destFilePath);
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class DownloadPublicObject method downloadPublicObject.
public static void downloadPublicObject(String bucketName, String publicObjectName, Path destFilePath) {
// The name of the bucket to access
// String bucketName = "my-bucket";
// The name of the remote public file to download
// String publicObjectName = "publicfile.txt";
// The path to which the file should be downloaded
// Path destFilePath = Paths.get("/local/path/to/file.txt");
// Instantiate an anonymous Google Cloud Storage client, which can only access public files
Storage storage = StorageOptions.getUnauthenticatedInstance().getService();
Blob blob = storage.get(BlobId.of(bucketName, publicObjectName));
blob.downloadTo(destFilePath);
System.out.println("Downloaded public object " + publicObjectName + " from bucket name " + bucketName + " to " + destFilePath);
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class ListObjectsWithPrefix method listObjectsWithPrefix.
public static void listObjectsWithPrefix(String projectId, String bucketName, String directoryPrefix) {
// The ID of your GCP project
// String projectId = "your-project-id";
// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";
// The directory prefix to search for
// String directoryPrefix = "myDirectory/"
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
/**
* Using the Storage.BlobListOption.currentDirectory() option here causes the results to display
* in a "directory-like" mode, showing what objects are in the directory you've specified, as
* well as what other directories exist in that directory. For example, given these blobs:
*
* <p>a/1.txt a/b/2.txt a/b/3.txt
*
* <p>If you specify prefix = "a/" and don't use Storage.BlobListOption.currentDirectory(),
* you'll get back:
*
* <p>a/1.txt a/b/2.txt a/b/3.txt
*
* <p>However, if you specify prefix = "a/" and do use
* Storage.BlobListOption.currentDirectory(), you'll get back:
*
* <p>a/1.txt a/b/
*
* <p>Because a/1.txt is the only file in the a/ directory and a/b/ is a directory inside the
* /a/ directory.
*/
Page<Blob> blobs = storage.list(bucketName, Storage.BlobListOption.prefix(directoryPrefix), Storage.BlobListOption.currentDirectory());
for (Blob blob : blobs.iterateAll()) {
System.out.println(blob.getName());
}
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class MoveObject method moveObject.
public static void moveObject(String projectId, String sourceBucketName, String sourceObjectName, String targetBucketName, String targetObjectName) {
// The ID of your GCP project
// String projectId = "your-project-id";
// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";
// The ID of your GCS object
// String sourceObjectName = "your-object-name";
// The ID of the bucket to move the object objectName to
// String targetBucketName = "target-object-bucket"
// The ID of your GCS object
// String targetObjectName = "your-new-object-name";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
Blob blob = storage.get(sourceBucketName, sourceObjectName);
// Write a copy of the object to the target bucket
CopyWriter copyWriter = blob.copyTo(targetBucketName, targetObjectName);
Blob copiedBlob = copyWriter.getResult();
// Delete the original blob now that we've copied to where we want it, finishing the "move"
// operation
blob.delete();
System.out.println("Moved object " + sourceObjectName + " from bucket " + sourceBucketName + " to " + targetObjectName + " in bucket " + copiedBlob.getBucket());
}
use of com.google.cloud.storage.Storage in project google-cloud-java by GoogleCloudPlatform.
the class ChangeObjectStorageClass method changeObjectStorageClass.
public static void changeObjectStorageClass(String projectId, String bucketName, String objectName) {
// The ID of your GCP project
// String projectId = "your-project-id";
// The ID of your GCS bucket
// String bucketName = "your-unique-bucket-name";
// The ID of your GCS object
// String objectName = "your-object-name";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);
// See the StorageClass documentation for other valid storage classes:
// https://googleapis.dev/java/google-cloud-clients/latest/com/google/cloud/storage/StorageClass.html
StorageClass storageClass = StorageClass.COLDLINE;
// You can't change an object's storage class directly, the only way is to rewrite the object
// with the
// desired storage class
Storage.CopyRequest request = Storage.CopyRequest.newBuilder().setSource(blobId).setTarget(BlobInfo.newBuilder(blobId).setStorageClass(storageClass).build()).build();
Blob updatedBlob = storage.copy(request).getResult();
System.out.println("Object " + objectName + " in bucket " + bucketName + " had its storage class set to " + updatedBlob.getStorageClass().name());
}
Aggregations