Search in sources :

Example 1 with Insert

use of com.google.api.services.storage.Storage.Objects.Insert in project google-cloud-java by GoogleCloudPlatform.

the class HttpStorageRpc method open.

@Override
public String open(StorageObject object, Map<Option, ?> options) {
    try {
        Insert req = storage.objects().insert(object.getBucket(), object);
        GenericUrl url = req.buildHttpRequest().getUrl();
        String scheme = url.getScheme();
        String host = url.getHost();
        String path = "/upload" + url.getRawPath();
        url = new GenericUrl(scheme + "://" + host + path);
        url.set("uploadType", "resumable");
        url.set("name", object.getName());
        for (Option option : options.keySet()) {
            Object content = option.get(options);
            if (content != null) {
                url.set(option.value(), content.toString());
            }
        }
        JsonFactory jsonFactory = storage.getJsonFactory();
        HttpRequestFactory requestFactory = storage.getRequestFactory();
        HttpRequest httpRequest = requestFactory.buildPostRequest(url, new JsonHttpContent(jsonFactory, object));
        HttpHeaders requestHeaders = httpRequest.getHeaders();
        requestHeaders.set("X-Upload-Content-Type", firstNonNull(object.getContentType(), "application/octet-stream"));
        String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options);
        if (key != null) {
            BaseEncoding base64 = BaseEncoding.base64();
            HashFunction hashFunction = Hashing.sha256();
            requestHeaders.set("x-goog-encryption-algorithm", "AES256");
            requestHeaders.set("x-goog-encryption-key", key);
            requestHeaders.set("x-goog-encryption-key-sha256", base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes()));
        }
        HttpResponse response = httpRequest.execute();
        if (response.getStatusCode() != 200) {
            GoogleJsonError error = new GoogleJsonError();
            error.setCode(response.getStatusCode());
            error.setMessage(response.getStatusMessage());
            throw translate(error);
        }
        return response.getHeaders().getLocation();
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonFactory(com.google.api.client.json.JsonFactory) HttpResponse(com.google.api.client.http.HttpResponse) LowLevelHttpResponse(com.google.api.client.http.LowLevelHttpResponse) GenericUrl(com.google.api.client.http.GenericUrl) JsonHttpContent(com.google.api.client.http.json.JsonHttpContent) IOException(java.io.IOException) Insert(com.google.api.services.storage.Storage.Objects.Insert) BaseEncoding(com.google.common.io.BaseEncoding) HashFunction(com.google.common.hash.HashFunction) StorageObject(com.google.api.services.storage.model.StorageObject) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Example 2 with Insert

use of com.google.api.services.storage.Storage.Objects.Insert in project google-cloud-java by GoogleCloudPlatform.

the class HttpStorageRpc method create.

@Override
public StorageObject create(StorageObject storageObject, final InputStream content, Map<Option, ?> options) {
    try {
        Storage.Objects.Insert insert = storage.objects().insert(storageObject.getBucket(), storageObject, new InputStreamContent(storageObject.getContentType(), content));
        insert.getMediaHttpUploader().setDirectUploadEnabled(true);
        setEncryptionHeaders(insert.getRequestHeaders(), ENCRYPTION_KEY_PREFIX, options);
        return insert.setProjection(DEFAULT_PROJECTION).setPredefinedAcl(Option.PREDEFINED_ACL.getString(options)).setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options)).setIfMetagenerationNotMatch(Option.IF_METAGENERATION_NOT_MATCH.getLong(options)).setIfGenerationMatch(Option.IF_GENERATION_MATCH.getLong(options)).setIfGenerationNotMatch(Option.IF_GENERATION_NOT_MATCH.getLong(options)).execute();
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : Insert(com.google.api.services.storage.Storage.Objects.Insert) InputStreamContent(com.google.api.client.http.InputStreamContent) Objects(com.google.api.services.storage.model.Objects) IOException(java.io.IOException)

Aggregations

Insert (com.google.api.services.storage.Storage.Objects.Insert)2 IOException (java.io.IOException)2 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)1 GenericUrl (com.google.api.client.http.GenericUrl)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 HttpResponse (com.google.api.client.http.HttpResponse)1 InputStreamContent (com.google.api.client.http.InputStreamContent)1 LowLevelHttpResponse (com.google.api.client.http.LowLevelHttpResponse)1 JsonHttpContent (com.google.api.client.http.json.JsonHttpContent)1 JsonFactory (com.google.api.client.json.JsonFactory)1 Objects (com.google.api.services.storage.model.Objects)1 StorageObject (com.google.api.services.storage.model.StorageObject)1 HashFunction (com.google.common.hash.HashFunction)1 BaseEncoding (com.google.common.io.BaseEncoding)1