ByteArrayContent content = new ByteArrayContent("application/json", bytes); storage.objects().insert(bucketName, object, content).execute();
InputStreamContent mediaContent = new InputStreamContent("text/plain", data); Storage.Objects.Insert insertObject = storage.objects().insert(bucketName, null, mediaContent).setName(objectName); insertObject.getMediaHttpUploader().setDisableGZipContent(true); httpHeaders.set("x-goog-encryption-key-sha256", base64CseKeyHash); insertObject.setRequestHeaders(httpHeaders); insertObject.execute(); } catch (GoogleJsonResponseException e) { System.out.println("Error uploading: " + e.getContent());
storageObject, new InputStreamContent(storageObject.getContentType(), content)); insert.getMediaHttpUploader().setDirectUploadEnabled(true); Boolean disableGzipContent = Option.IF_DISABLE_GZIP_CONTENT.getBoolean(options); if (disableGzipContent != null) { insert.setDisableGZipContent(disableGzipContent); 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)) .setUserProject(Option.USER_PROJECT.getString(options)) .setKmsKeyName(Option.KMS_KEY_NAME.getString(options)) .execute(); } catch (IOException ex) { span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage()));
insertObject.setIfGenerationMatch( getWriteGeneration(resourceId, options.overwriteExisting())); StorageObject result = insertObject.execute(); overwriteGeneration = Optional.of(result.getGeneration()); } catch (IOException ioe) {
.insert ( bucketName, objectMetadata, contentStream ); insertRequest.execute ();
() -> { try { insertObject.execute(); logger.atFine().log("Successfully inserted %s", resourceId); } catch (IOException ioe) {
storageObject, new InputStreamContent(storageObject.getContentType(), content)); insert.getMediaHttpUploader().setDirectUploadEnabled(true); Boolean disableGzipContent = Option.IF_DISABLE_GZIP_CONTENT.getBoolean(options); if (disableGzipContent != null) { insert.setDisableGZipContent(disableGzipContent); 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)) .setUserProject(Option.USER_PROJECT.getString(options)) .setKmsKeyName(Option.KMS_KEY_NAME.getString(options)) .execute(); } catch (IOException ex) { span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage()));
Storage.Objects.Insert insertRequest = getService().objects().insert(bucket, objectMetadata, contentStream); insertRequest.execute();
/** * Helper for creating a Storage.Objects.Copy object ready for dispatch given a bucket and object * for an empty object to be created. Caller must already verify that {@code resourceId} * represents a StorageObject and not a bucket. */ private Storage.Objects.Insert prepareEmptyInsert( StorageResourceId resourceId, CreateObjectOptions createObjectOptions) throws IOException { StorageObject object = new StorageObject(); object.setName(resourceId.getObjectName()); Map<String, String> rewrittenMetadata = encodeMetadata(createObjectOptions.getMetadata()); object.setMetadata(rewrittenMetadata); // Ideally we'd use EmptyContent, but Storage requires an AbstractInputStreamContent and not // just an HttpContent, so we'll just use the next easiest thing. ByteArrayContent emptyContent = new ByteArrayContent(createObjectOptions.getContentType(), new byte[0]); Storage.Objects.Insert insertObject = configureRequest( gcs.objects().insert(resourceId.getBucketName(), object, emptyContent), resourceId.getBucketName()); insertObject.setDisableGZipContent(true); clientRequestHelper.setDirectUploadEnabled(insertObject, true); if (resourceId.hasGenerationId()) { insertObject.setIfGenerationMatch(resourceId.getGenerationId()); } else if (!createObjectOptions.overwriteExisting()) { insertObject.setIfGenerationMatch(0L); } return insertObject; }
StorageObject metaData = new StorageObject().setName(filename); Storage.Objects.Insert insert = storage.objects().insert("nitro-resources", metaData, content); insert.execute();
@Override public Insert createRequest(InputStreamContent inputStream) throws IOException { // Create object with the given name and metadata. StorageObject object = new StorageObject() .setMetadata(metadata) .setName(objectName); Insert insert = gcs.objects().insert(bucketName, object, inputStream); writeConditions.apply(insert); if (insert.getMediaHttpUploader() != null) { insert.getMediaHttpUploader().setDirectUploadEnabled(isDirectUploadEnabled()); insert.getMediaHttpUploader().setProgressListener( new LoggingMediaHttpUploaderProgressListener(this.objectName, MIN_LOGGING_INTERVAL_MS)); } insert.setName(objectName); if (kmsKeyName != null) { insert.setKmsKeyName(kmsKeyName); } return insert; }
@Override public void uploadCache(JobOutputProcessor outputProcessor, String cachePath) { try { outputProcessor.output("Uploading cache file " + cacheFileName + " to google storage\n"); Storage client = createClient(); File uploadFile = new File(cachePath); InputStreamContent contentStream = new InputStreamContent( null, new FileInputStream(uploadFile)); contentStream.setLength(uploadFile.length()); StorageObject objectMetadata = new StorageObject() .setName(cacheFileName); Storage.Objects.Insert insertRequest = client.objects().insert( settings.bucketName, objectMetadata, contentStream); insertRequest.execute(); outputProcessor.output("Cache uploaded\n"); } catch (GeneralSecurityException | IOException e) { outputProcessor.output("Error upload cache: " + e.getMessage() + "\n"); } }
@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); return insert.setProjection(DEFAULT_PROJECTION) .setPredefinedAcl(PREDEFINED_ACL.getString(options)) .setIfMetagenerationMatch(IF_METAGENERATION_MATCH.getLong(options)) .setIfMetagenerationNotMatch(IF_METAGENERATION_NOT_MATCH.getLong(options)) .setIfGenerationMatch(IF_GENERATION_MATCH.getLong(options)) .setIfGenerationNotMatch(IF_GENERATION_NOT_MATCH.getLong(options)) .execute(); } catch (IOException ex) { throw translate(ex); } }
public static StorageObject uploadSimpleImageAndMakeItPublic( Storage storage, String bucketName, String objectName, InputStream imageData) throws IOException { InputStreamContent mediaContent = new InputStreamContent("image/png", imageData); Storage.Objects.Insert insertObject = storage.objects() .insert(bucketName, null, mediaContent).setName(objectName); insertObject.getMediaHttpUploader().setDisableGZipContent(true); StorageObject uploadedImage = insertObject.execute(); // new acl to make it public ObjectAccessControl publicACLItem = new ObjectAccessControl(); publicACLItem.setEntity("allUsers"); publicACLItem.setRole("READER"); Insert insert = storage.objectAccessControls().insert(bucketName, objectName, publicACLItem); insert.execute(); return uploadedImage; }
@Override public void store(BuildCacheKey buildCacheKey, BuildCacheEntryWriter buildCacheEntryWriter) throws BuildCacheException { final String bucketPath = getBucketPath(buildCacheKey); logger.info("Start storing cache entry '{}' in GCS bucket", bucketPath); try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { buildCacheEntryWriter.writeTo(os); try (InputStream is = new ByteArrayInputStream(os.toByteArray())) { InputStreamContent content = new InputStreamContent(BUILD_CACHE_CONTENT_TYPE, is); StorageObject metadataObject = new StorageObject().setMetadata(new HashMap<>()); Storage.Objects.Insert insertObject = storage.objects().insert(bucketName, metadataObject, content).setName(bucketPath); insertObject.getMediaHttpUploader().setDisableGZipContent(true); insertObject.execute(); } } catch (IOException e) { throw new BuildCacheException("Error while storing cache object in GCS bucket", e); } }