use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class StreamObjectUpload method streamObjectUpload.
public static void streamObjectUpload(String projectId, String bucketName, String objectName, String contents) throws IOException {
// 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 string of contents you wish to upload
// String contents = "Hello world!";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
byte[] content = contents.getBytes(StandardCharsets.UTF_8);
try (WriteChannel writer = storage.writer(blobInfo)) {
writer.write(ByteBuffer.wrap(content));
System.out.println("Wrote to " + objectName + " in bucket " + bucketName + " using a WriteChannel.");
}
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class UploadObject method uploadObject.
public static void uploadObject(String projectId, String bucketName, String objectName, String filePath) throws IOException {
// 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 your file to upload
// String filePath = "path/to/your/file"
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)));
System.out.println("File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName);
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class UploadEncryptedObject method uploadEncryptedObject.
public static void uploadEncryptedObject(String projectId, String bucketName, String objectName, String filePath, String encryptionKey) throws IOException {
// 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 your file to upload
// String filePath = "path/to/your/file"
// The key to encrypt the object with
// String encryptionKey = "TIbv/fjexq+VmtXzAlc63J4z5kFmWJ6NdAPQulQBT7g=";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
BlobId blobId = BlobId.of(bucketName, objectName);
BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
storage.create(blobInfo, Files.readAllBytes(Paths.get(filePath)), Storage.BlobTargetOption.encryptionKey(encryptionKey));
System.out.println("File " + filePath + " uploaded to bucket " + bucketName + " as " + objectName + " with supplied encryption key");
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class GenerateV4GetObjectSignedUrl method generateV4GetObjectSignedUrl.
/**
* Signing a URL requires Credentials which implement ServiceAccountSigner. These can be set
* explicitly using the Storage.SignUrlOption.signWith(ServiceAccountSigner) option. If you don't,
* you could also pass a service account signer to StorageOptions, i.e.
* StorageOptions().newBuilder().setCredentials(ServiceAccountSignerCredentials). In this example,
* neither of these options are used, which means the following code only works when the
* credentials are defined via the environment variable GOOGLE_APPLICATION_CREDENTIALS, and those
* credentials are authorized to sign a URL. See the documentation for Storage.signUrl for more
* details.
*/
public static void generateV4GetObjectSignedUrl(String projectId, String bucketName, String objectName) throws StorageException {
// String projectId = "my-project-id";
// String bucketName = "my-bucket";
// String objectName = "my-object";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
// Define resource
BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(bucketName, objectName)).build();
URL url = storage.signUrl(blobInfo, 15, TimeUnit.MINUTES, Storage.SignUrlOption.withV4Signature());
System.out.println("Generated GET signed URL:");
System.out.println(url);
System.out.println("You can use this URL with any user agent, for example:");
System.out.println("curl '" + url + "'");
}
use of com.google.cloud.storage.BlobInfo in project google-cloud-java by GoogleCloudPlatform.
the class GenerateV4PutObjectSignedUrl method generateV4GPutObjectSignedUrl.
/**
* Signing a URL requires Credentials which implement ServiceAccountSigner. These can be set
* explicitly using the Storage.SignUrlOption.signWith(ServiceAccountSigner) option. If you don't,
* you could also pass a service account signer to StorageOptions, i.e.
* StorageOptions().newBuilder().setCredentials(ServiceAccountSignerCredentials). In this example,
* neither of these options are used, which means the following code only works when the
* credentials are defined via the environment variable GOOGLE_APPLICATION_CREDENTIALS, and those
* credentials are authorized to sign a URL. See the documentation for Storage.signUrl for more
* details.
*/
public static void generateV4GPutObjectSignedUrl(String projectId, String bucketName, String objectName) throws StorageException {
// String projectId = "my-project-id";
// String bucketName = "my-bucket";
// String objectName = "my-object";
Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();
// Define Resource
BlobInfo blobInfo = BlobInfo.newBuilder(BlobId.of(bucketName, objectName)).build();
// Generate Signed URL
Map<String, String> extensionHeaders = new HashMap<>();
extensionHeaders.put("Content-Type", "application/octet-stream");
URL url = storage.signUrl(blobInfo, 15, TimeUnit.MINUTES, Storage.SignUrlOption.httpMethod(HttpMethod.PUT), Storage.SignUrlOption.withExtHeaders(extensionHeaders), Storage.SignUrlOption.withV4Signature());
System.out.println("Generated PUT signed URL:");
System.out.println(url);
System.out.println("You can use this URL with any user agent, for example:");
System.out.println("curl -X PUT -H 'Content-Type: application/octet-stream' --upload-file my-file '" + url + "'");
}
Aggregations