Java Code Examples for com.google.cloud.storage.Storage#create()

The following examples show how to use com.google.cloud.storage.Storage#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImportProductSetsIT.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
  // Create the product set csv file locally and upload it to GCS
  // This is so that there is a unique product set ID for all java version tests.
  Storage storage = StorageOptions.newBuilder().setProjectId(PROJECT_ID).build().getService();
  BlobId blobId = BlobId.of(PROJECT_ID, FILEPATH);
  BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build();
  String csvContents =
      "\"gs://cloud-samples-data/vision/product_search/shoes_1.jpg\","
          + String.format("\"%s\",", IMAGE_URI_1)
          + String.format("\"%s\",", PRODUCT_SET_ID)
          + String.format("\"%s\",", PRODUCT_ID_1)
          + "\"apparel\",,\"style=womens\",\"0.1,0.1,0.9,0.1,0.9,0.9,0.1,0.9\"";
  blob = storage.create(blobInfo, csvContents.getBytes());

  bout = new ByteArrayOutputStream();
  out = new PrintStream(bout);
  System.setOut(out);
}
 
Example 2
Source File: UploadObject.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: MultipartUploader.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param storage an initialized {@link Storage} instance
 * @param bucket the name of the bucket
 * @param destination the the destination (relative to the bucket)
 * @param contents the stream of data to store
 * @return the successfully stored {@link Blob}
 * @throws BlobStoreException if any part of the upload failed
 */
@Override
@Guarded(by = STARTED)
public Blob upload(final Storage storage, final String bucket, final String destination, final InputStream contents) {
  if(isParallel()) {
    return parallelUpload(storage, bucket, destination, contents);
  }
  log.debug("Starting upload for destination {} in bucket {}", destination, bucket);
  BlobInfo blobInfo = BlobInfo.newBuilder(bucket, destination).build();
  Blob result = storage.create(blobInfo, contents, BlobWriteOption.disableGzipContent());
  log.debug("Upload of {} complete", destination);
  return result;
}
 
Example 4
Source File: DLPTemplateHelper.java    From dlp-dataflow-deidentification with Apache License 2.0 5 votes vote down vote up
public static BlobId uploadConfig(String contents, String gcsPath, String fileName) {
  GcsPath path = GcsPath.fromUri(URI.create(gcsPath));
  Storage storage = StorageOptions.getDefaultInstance().getService();
  BlobId blobId = BlobId.of(path.getBucket(), fileName);
  BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("application/json").build();
  Blob blob = storage.create(blobInfo, contents.getBytes(UTF_8));
  return blob.getBlobId();
}
 
Example 5
Source File: GcsPluginUtils.java    From nomulus with Apache License 2.0 5 votes vote down vote up
static void uploadFileToGcs(
    Storage storage, String bucket, Path path, Supplier<byte[]> dataSupplier) {
  // Replace Windows file separators with forward slashes.
  String filename = path.toString().replace(File.separator, "/");
  storage.create(
      BlobInfo.newBuilder(bucket, filename).setContentType(getContentType(filename)).build(),
      dataSupplier.get());
}
 
Example 6
Source File: QuickstartTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  bout = new ByteArrayOutputStream();
  System.setOut(new PrintStream(bout));

  Storage storage = StorageOptions.getDefaultInstance().getService();
  bucket = storage.create(BucketInfo.of(BUCKET_NAME));
  blob = bucket.create(JOB_FILE_NAME, SORT_CODE.getBytes(UTF_8), "text/plain");
}
 
Example 7
Source File: QuickstartSample.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) throws Exception {
  // Instantiates a client
  Storage storage = StorageOptions.getDefaultInstance().getService();

  // The name for the new bucket
  String bucketName = args[0];  // "my-new-bucket";

  // Creates the new bucket
  Bucket bucket = storage.create(BucketInfo.of(bucketName));

  System.out.printf("Bucket %s created.%n", bucket.getName());
}
 
Example 8
Source File: CreateBucketWithStorageClassAndLocation.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void createBucketWithStorageClassAndLocation(String projectId, String bucketName) {
  // The ID of your GCP project
  // String projectId = "your-project-id";

  // The ID to give your GCS bucket
  // String bucketName = "your-unique-bucket-name";

  Storage storage = StorageOptions.newBuilder().setProjectId(projectId).build().getService();

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

  // See this documentation for other valid locations:
  // http://g.co/cloud/storage/docs/bucket-locations#location-mr
  String location = "asia";

  Bucket bucket =
      storage.create(
          BucketInfo.newBuilder(bucketName)
              .setStorageClass(storageClass)
              .setLocation(location)
              .build());

  System.out.println(
      "Created bucket "
          + bucket.getName()
          + " in "
          + bucket.getLocation()
          + " with storage class "
          + bucket.getStorageClass());
}
 
Example 9
Source File: UploadEncryptedObject.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example 10
Source File: CreateAndListBucketsAndBlobs.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public static void main(String... args) {
  // Create a service object
  // Credentials are inferred from the environment.
  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
  Blob blob = bucket.create("my_blob_name", "a simple blob".getBytes(UTF_8), "text/plain");

  // Read the blob content from the server
  String blobContent = new String(blob.getContent(), UTF_8);

  // List all your buckets
  System.out.println("My buckets:");
  for (Bucket currentBucket : storage.list().iterateAll()) {
    System.out.println(currentBucket);
  }

  // List the blobs in a particular bucket
  System.out.println("My blobs:");
  for (Blob currentBlob : bucket.list().iterateAll()) {
    System.out.println(currentBlob);
  }
}