/** {@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();
.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();
insert.setName(file.getName()); insert.execute(); } finally { stream.close();
StorageObject metaData = new StorageObject().setName(filename); Storage.Objects.Insert insert = storage.objects().insert("nitro-resources", metaData, content); insert.execute();
@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 createEmptyObject(StorageResourceId resourceId, CreateObjectOptions options) throws IOException { Preconditions.checkArgument( resourceId.isStorageObject(), "Expected full StorageObject id, got %s", resourceId); Storage.Objects.Insert insertObject = prepareEmptyInsert(resourceId, options); try { insertObject.execute(); } catch (IOException ioe) { if (canIgnoreExceptionForEmptyObject(ioe, resourceId, options)) { logger.atInfo().withCause(ioe).log( "Ignoring exception; verified object already exists with desired state."); } else { throw ioe; } } }
@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); } }
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; }
Storage.Objects.Insert insert = storage.objects().insert(bucket.getName(), so, content); insert.getMediaHttpUploader().setDirectUploadEnabled(true); insert.execute();
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); } }
insertObject.execute(); } catch (GoogleJsonResponseException e) { System.out.println("Error uploading: " + e.getContent());
/** * 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 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(); }
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 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(); }