Scope scope = tracer.withSpan(span); try { Insert req = storage.objects().insert(object.getBucket(), object); GenericUrl url = req.buildHttpRequest().getUrl(); String scheme = url.getScheme();
/** {@inheritDoc} */ @Override public void registerAddresses(Collection<InetSocketAddress> addrs) throws IgniteSpiException { assert !F.isEmpty(addrs); init(); for (InetSocketAddress addr : addrs) { String key = keyFromAddr(addr); StorageObject object = new StorageObject(); object.setBucket(bucketName); object.setName(key); InputStreamContent content = new InputStreamContent("application/octet-stream", OBJECT_CONTENT); content.setLength(OBJECT_CONTENT.available()); try { Storage.Objects.Insert insertObject = storage.objects().insert(bucketName, object, content); insertObject.execute(); } catch (Exception e) { throw new IgniteSpiException("Failed to put entry [bucketName=" + bucketName + ", entry=" + key + ']', e); } } }
ByteArrayContent content = new ByteArrayContent("application/json", bytes); storage.objects().insert(bucketName, object, content).execute();
@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();
storage .objects() .insert( storageObject.getBucket(), storageObject,
stream); Storage.Objects.Insert insert = storage.objects().insert( bucketName, null, content); insert.setName(file.getName());
/** * 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; }
@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); } }
@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); } }
private void writeTextObject(String name, String contents) { try { byte[] bytes = contents.getBytes(); StorageObject object = new StorageObject() .setBucket(spinconfigBucket) .setName(name); ByteArrayContent content = new ByteArrayContent("application/text", bytes); storage.objects().insert(spinconfigBucket, object, content).execute(); } catch (IOException e) { log.error("Failed to write new object " + name, e); throw new HalException(new ProblemBuilder(Severity.FATAL, "Failed to write to " + name + ": " + e.getMessage()).build()); } } }
void writeBytes(String name, byte[] contents) { name = String.join("/", rootFolder, name); try { StorageObject object = new StorageObject() .setBucket(bucketId) .setName(name); ByteArrayContent content = new ByteArrayContent("application/text", contents); storage.objects().insert(bucketId, object, content).execute(); } catch (IOException e) { throw new HalException(Problem.Severity.FATAL, "Failed to write to " + name + ": " + e.getMessage(), e); } }
void writeBytes(String name, byte[] contents) { name = String.join("/", rootFolder, name); try { StorageObject object = new StorageObject() .setBucket(bucketId) .setName(name); ByteArrayContent content = new ByteArrayContent("application/text", contents); storage.objects().insert(bucketId, object, content).execute(); } catch (IOException e) { throw new HalException(Problem.Severity.FATAL, "Failed to write to " + name + ": " + e.getMessage(), e); } }
InputStreamContent mediaContent = new InputStreamContent("text/plain", data); Storage.Objects.Insert insertObject = storage.objects().insert(bucketName, null, mediaContent).setName(objectName);
/** * Uploads data to an object in a bucket. * * @param name the name of the destination object. * @param contentType the MIME type of the data. * @param file the file to upload. * @param bucketName the name of the bucket to create the object in. */ public static void uploadFile( String name, String contentType, File file, String bucketName) throws IOException, GeneralSecurityException { InputStreamContent contentStream = new InputStreamContent( contentType, new FileInputStream(file)); // Setting the length improves upload performance contentStream.setLength(file.length()); StorageObject objectMetadata = new StorageObject() // Set the destination object name .setName(name) // Set the access control list to publicly read-only .setAcl(Arrays.asList( new ObjectAccessControl().setEntity("allUsers").setRole("READER"))); // Do the insert Storage client = StorageFactory.getService(); Storage.Objects.Insert insertRequest = client.objects().insert( bucketName, objectMetadata, contentStream); insertRequest.execute(); } // [END upload_stream]
public static StorageObject uploadWithMetadata(Storage storage, StorageObject object, InputStream data) throws IOException { InputStreamContent mediaContent = new InputStreamContent(object.getContentType(), data); Storage.Objects.Insert insertObject = storage.objects().insert(object.getBucket(), object, mediaContent); insertObject.getMediaHttpUploader().setDisableGZipContent(true); return insertObject.execute(); }
public static StorageObject uploadSimple(Storage storage, String bucketName, String objectName, InputStream data, String contentType) throws IOException { InputStreamContent mediaContent = new InputStreamContent(contentType, data); Storage.Objects.Insert insertObject = storage.objects().insert(bucketName, null, mediaContent) .setName(objectName); // The media uploader gzips content by default, and alters the Content-Encoding accordingly. // GCS dutifully stores content as-uploaded. This line disables the media uploader behavior, // so the service stores exactly what is in the InputStream, without transformation. insertObject.getMediaHttpUploader().setDisableGZipContent(true); return insertObject.execute(); }
storage .objects() .insert( storageObject.getBucket(), storageObject,
public void insert(final String bucket, final String path, AbstractInputStreamContent mediaContent) throws IOException { Storage.Objects.Insert insertObject = storage.objects().insert(bucket, null, mediaContent); insertObject.setName(path); insertObject.getMediaHttpUploader().setDirectUploadEnabled(false); insertObject.execute(); }