public long size(final String bucket, final String path) throws IOException { return storage.objects().get(bucket, path).execute().getSize().longValue(); }
get = constructObjectResourceHandle().get(this.srcBucketName, remotePath.toString()); } catch (IOException e) { throw new BackupRestoreException(
storage .objects() .get(from.getBucket(), from.getName()) .setGeneration(from.getGeneration()) .setIfMetagenerationMatch(Option.IF_METAGENERATION_MATCH.getLong(options))
private Storage.Objects.Get getRequest(StorageObject object, Map<Option, ?> options) throws IOException { return storage.objects() .get(object.getBucket(), object.getName()) .setGeneration(object.getGeneration()) .setProjection(DEFAULT_PROJECTION) .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)) .setFields(FIELDS.getString(options)); }
private InputStream getContents(String objectName) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); log.info("Getting object contents of " + objectName); try { applicationDefaultGoogleStorage.objects().get(spinconfigBucket, objectName).executeMediaAndDownloadTo(output); } catch (IOException e) { log.debug("Getting object contents of {} failed. Retrying with no authentication.", objectName, e); output = new ByteArrayOutputStream(); unauthenticatedGoogleStorage.objects().get(spinconfigBucket, objectName).executeMediaAndDownloadTo(output); } return new ByteArrayInputStream(output.toByteArray()); }
@Test public void testWhenTracingMatches() throws Exception { String[] args = new String[] {STORAGE_GET_TRACE}; GcsOptions options = PipelineOptionsFactory.fromArgs(args).as(GcsOptions.class); options.setGcpCredential(new TestCredential()); assertNotNull(options.getGoogleApiTrace()); Storage.Objects.Get request = Transport.newStorageClient(options).build().objects().get("testBucketId", "testObjectId"); assertEquals("GetTraceDestination", request.get("$trace")); }
public SeekableGCSStream(Storage.Objects client, String name) throws IOException { LOG.info("Creating SeekableGCSStream: " + name); this.client = client; object = uriToStorageObject(name); get = this.client.get(object.getBucket(), object.getName()); seek(0); }
/** * Retrieve the whole file (to memory). * * @throws IOException */ public InputStream getWholeObject(String bucket, String fname) throws IOException { Storage.Objects.Get getObject = storage.objects().get(bucket, fname); return getObject.executeMediaAsInputStream(); }
httpHeaders.set("x-goog-encryption-key-sha256", base64CseKeyHash); Storage.Objects.Get getObject = storage.objects().get(bucketName, objectName);
@Override public <T> T loadObject(String accountName, ObjectType objectType, String objectKey) throws IllegalArgumentException, NotFoundException { GoogleNamedAccountCredentials credentials = (GoogleNamedAccountCredentials)accountCredentialsRepository .getOne(accountName) .orElseThrow(() -> new IllegalArgumentException("Unable to resolve account " + accountName + ".")); Storage storage = credentials.getStorage(); String bucketName = credentials.getBucket(); StorageObject item; try { item = resolveSingularItem(objectType, objectKey, credentials, storage, bucketName); } catch (IllegalArgumentException e) { throw new NotFoundException(e.getMessage()); } try { StorageObject storageObject = storage.objects().get(bucketName, item.getName()).execute(); return deserialize(storage, storageObject, objectType.getTypeReference()); } catch (IOException e) { if (e instanceof HttpResponseException) { HttpResponseException hre = (HttpResponseException)e; log.error("Failed to load {} {}: {} {}", objectType.getGroup(), objectKey, hre.getStatusCode(), hre.getStatusMessage()); if (hre.getStatusCode() == 404) { throw new NotFoundException("No file at path " + item.getName() + "."); } } throw new IllegalStateException(e); } }
/** * This shows how to download a portion of an object. Especially useful for * resuming after a download fails, but can also be used to download in * parallel. */ public static void downloadRangeToOutputStream(Storage storage, String bucketName, String objectName, long firstBytePos, long lastBytePos, OutputStream data) throws IOException { Storage.Objects.Get getObject = storage.objects().get(bucketName, objectName); // Remove cast after https://github.com/google/google-api-java-client/issues/937 is addressed. getObject.getMediaHttpDownloader().setDirectDownloadEnabled(!IS_APP_ENGINE) .setContentRange(firstBytePos, (int) lastBytePos); getObject.executeMediaAndDownloadTo(data); }
public static InputStream download(Storage storage, String bucketName, String objectName) throws IOException { Storage.Objects.Get getObject = storage.objects().get(bucketName, objectName); getObject.getMediaHttpDownloader().setDirectDownloadEnabled(!IS_APP_ENGINE); return getObject.executeMediaAsInputStream(); }
public static StorageObject get(Storage storage, String bucketName, String objectName) throws IOException { Storage.Objects.Get getObject = storage.objects().get(bucketName, objectName); return getObject.execute(); }
public static void downloadToOutputStream(Storage storage, String bucketName, String objectName, OutputStream data) throws IOException { Storage.Objects.Get getObject = storage.objects().get(bucketName, objectName); getObject.getMediaHttpDownloader().setDirectDownloadEnabled(!IS_APP_ENGINE); getObject.executeMediaAndDownloadTo(data); }
private <T> T deserialize(Storage storage, StorageObject object, TypeReference typeReference) throws IOException { ByteArrayOutputStream output = new java.io.ByteArrayOutputStream(); Storage.Objects.Get getter = storage.objects().get(object.getBucket(), object.getName()); getter.executeMediaAndDownloadTo(output); String json = output.toString("UTF8"); return kayentaObjectMapper.readValue(json, typeReference); }
@Override public byte[] load(StorageObject from, Map<Option, ?> options) { Span span = startSpan(HttpStorageRpcSpans.SPAN_NAME_LOAD); Scope scope = tracer.withSpan(span); try { Storage.Objects.Get getRequest = storage .objects() .get(from.getBucket(), from.getName()) .setGeneration(from.getGeneration()) .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)); setEncryptionHeaders(getRequest.getRequestHeaders(), ENCRYPTION_KEY_PREFIX, options); ByteArrayOutputStream out = new ByteArrayOutputStream(); getRequest.executeMedia().download(out); return out.toByteArray(); } catch (IOException ex) { span.setStatus(Status.UNKNOWN.withDescription(ex.getMessage())); throw translate(ex); } finally { scope.close(); span.end(); } }
private Storage.Objects.Get getCall(StorageObject object, Map<Option, ?> options) throws IOException { return storage .objects() .get(object.getBucket(), object.getName()) .setGeneration(object.getGeneration()) .setProjection(DEFAULT_PROJECTION) .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)) .setFields(Option.FIELDS.getString(options)) .setUserProject(Option.USER_PROJECT.getString(options)); }
public String version(final String bucket, final String path) throws IOException { return storage.objects().get(bucket, path).execute().getEtag(); }
public boolean exists(final String bucket, final String path) { try { return storage.objects().get(bucket, path).executeUsingHead().isSuccessStatusCode(); } catch (Exception e) { return false; } }
public InputStream get(final String bucket, final String path, long start) throws IOException { final Get get = storage.objects().get(bucket, path); if (start > 0) { get.getMediaHttpDownloader().setBytesDownloaded(start); } get.getMediaHttpDownloader().setDirectDownloadEnabled(false); return get.executeMediaAsInputStream(); }