Search in sources :

Example 1 with GoogleCloudStorageOptions

use of com.google.cloud.hadoop.gcsio.GoogleCloudStorageOptions in project beam by apache.

the class GcsUtil method create.

/**
 * Creates an object in GCS and prepares for uploading its contents.
 *
 * @param path the GCS file to write to
 * @param options to be used for creating and configuring file upload
 * @return a WritableByteChannel that can be used to write data to the object.
 */
public WritableByteChannel create(GcsPath path, CreateOptions options) throws IOException {
    AsyncWriteChannelOptions wcOptions = googleCloudStorageOptions.getWriteChannelOptions();
    @Nullable Integer uploadBufferSizeBytes = options.getUploadBufferSizeBytes() != null ? options.getUploadBufferSizeBytes() : getUploadBufferSizeBytes();
    if (uploadBufferSizeBytes != null) {
        wcOptions = wcOptions.toBuilder().setUploadChunkSize(uploadBufferSizeBytes).build();
    }
    GoogleCloudStorageOptions newGoogleCloudStorageOptions = googleCloudStorageOptions.toBuilder().setWriteChannelOptions(wcOptions).build();
    GoogleCloudStorage gcpStorage = new GoogleCloudStorageImpl(newGoogleCloudStorageOptions, this.storageClient, this.credentials);
    StorageResourceId resourceId = new StorageResourceId(path.getBucket(), path.getObject(), // See {@link GoogleCloudStorage#create(StorageResourceId, GoogleCloudStorageOptions)}
    options.getExpectFileToNotExist() ? 0L : StorageResourceId.UNKNOWN_GENERATION_ID);
    CreateObjectOptions.Builder createBuilder = CreateObjectOptions.builder().setOverwriteExisting(true);
    if (options.getContentType() != null) {
        createBuilder = createBuilder.setContentType(options.getContentType());
    }
    HashMap<String, String> baseLabels = new HashMap<>();
    baseLabels.put(MonitoringInfoConstants.Labels.PTRANSFORM, "");
    baseLabels.put(MonitoringInfoConstants.Labels.SERVICE, "Storage");
    baseLabels.put(MonitoringInfoConstants.Labels.METHOD, "GcsInsert");
    baseLabels.put(MonitoringInfoConstants.Labels.RESOURCE, GcpResourceIdentifiers.cloudStorageBucket(path.getBucket()));
    baseLabels.put(MonitoringInfoConstants.Labels.GCS_PROJECT_ID, googleCloudStorageOptions.getProjectId());
    baseLabels.put(MonitoringInfoConstants.Labels.GCS_BUCKET, path.getBucket());
    ServiceCallMetric serviceCallMetric = new ServiceCallMetric(MonitoringInfoConstants.Urns.API_REQUEST_COUNT, baseLabels);
    try {
        WritableByteChannel channel = gcpStorage.create(resourceId, createBuilder.build());
        serviceCallMetric.call("ok");
        return channel;
    } catch (IOException e) {
        if (e.getCause() instanceof GoogleJsonResponseException) {
            serviceCallMetric.call(((GoogleJsonResponseException) e.getCause()).getDetails().getCode());
        }
        throw e;
    }
}
Also used : GoogleCloudStorage(com.google.cloud.hadoop.gcsio.GoogleCloudStorage) HashMap(java.util.HashMap) WritableByteChannel(java.nio.channels.WritableByteChannel) IOException(java.io.IOException) StorageResourceId(com.google.cloud.hadoop.gcsio.StorageResourceId) GoogleCloudStorageImpl(com.google.cloud.hadoop.gcsio.GoogleCloudStorageImpl) ServiceCallMetric(org.apache.beam.runners.core.metrics.ServiceCallMetric) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) AsyncWriteChannelOptions(com.google.cloud.hadoop.util.AsyncWriteChannelOptions) GoogleCloudStorageOptions(com.google.cloud.hadoop.gcsio.GoogleCloudStorageOptions) CreateObjectOptions(com.google.cloud.hadoop.gcsio.CreateObjectOptions) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 CreateObjectOptions (com.google.cloud.hadoop.gcsio.CreateObjectOptions)1 GoogleCloudStorage (com.google.cloud.hadoop.gcsio.GoogleCloudStorage)1 GoogleCloudStorageImpl (com.google.cloud.hadoop.gcsio.GoogleCloudStorageImpl)1 GoogleCloudStorageOptions (com.google.cloud.hadoop.gcsio.GoogleCloudStorageOptions)1 StorageResourceId (com.google.cloud.hadoop.gcsio.StorageResourceId)1 AsyncWriteChannelOptions (com.google.cloud.hadoop.util.AsyncWriteChannelOptions)1 IOException (java.io.IOException)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ServiceCallMetric (org.apache.beam.runners.core.metrics.ServiceCallMetric)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1