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); }
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()); }
getObject.executeMediaAndDownloadTo(out);
/** * Retrieve part of the file. * * Example thing you may want to do with the result: * String str = new String( Arrays.copyOfRange(out.toByteArray() ); * * @throws IOException */ public ByteArrayOutputStream getPartialObjectData(String bucket, String fname, long start, long endIncl, @Nullable ByteArrayOutputStream optionalOldOutputToReuse) throws IOException { ByteArrayOutputStream out; if (null == optionalOldOutputToReuse) { out = new ByteArrayOutputStream((int) (endIncl - start + 1)); } else { out = optionalOldOutputToReuse; out.reset(); } Storage.Objects.Get getObject = storage.objects().get(bucket, fname); getObject.setRequestHeaders(new HttpHeaders().setRange( String.format("bytes=%d-%d", start, endIncl))); getObject.getMediaHttpDownloader().setDirectDownloadEnabled(!IS_APP_ENGINE); getObject.executeMediaAndDownloadTo(out); if (out.size() != (endIncl - start + 1)) { String err = "getPartialObjectData failed! Expected " + (endIncl - start + 1) + " bytes, got " + out.size(); LOGGER.log(Level.WARNING, err); throw new IOException(err); } return out; }
/** * Retrieve part of the file. * * Example thing you may want to do with the result: * String str = new String( Arrays.copyOfRange(out.toByteArray() ); * * @throws IOException */ public ByteArrayOutputStream getPartialObjectData(String bucket, String fname, long start, long endIncl, @Nullable ByteArrayOutputStream optionalOldOutputToReuse) throws IOException { ByteArrayOutputStream out; if (null == optionalOldOutputToReuse) { out = new ByteArrayOutputStream((int) (endIncl - start + 1)); } else { out = optionalOldOutputToReuse; out.reset(); } Storage.Objects.Get getObject = storage.objects().get(bucket, fname); getObject.setRequestHeaders(new HttpHeaders().setRange( String.format("bytes=%d-%d", start, endIncl))); getObject.getMediaHttpDownloader().setDirectDownloadEnabled(!IS_APP_ENGINE); getObject.executeMediaAndDownloadTo(out); if (out.size() != (endIncl - start + 1)) { String err = "getPartialObjectData failed! Expected " + (endIncl - start + 1) + " bytes, got " + out.size(); LOGGER.log(Level.WARNING, err); throw new IOException(err); } return out; }
@Override public void downloadCache(JobOutputProcessor outputProcessor, DownloadedCacheHandler handler) { String tmpCacheFileName = "cache-" + UUID.randomUUID() + ".tar.gz"; File tmpDir = Files.createTempDir(); String tmpCacheFilePath = Paths.get(tmpDir.getAbsolutePath(), tmpCacheFileName).toString(); try { outputProcessor.output("Downloading cache file " + cacheFileName + " from google storage\n"); Storage client = createClient(); FileOutputStream os = new FileOutputStream(tmpCacheFilePath); client.objects().get(settings.bucketName, cacheFileName) .executeMediaAndDownloadTo(os); os.close(); handler.handleDownloadedCache(tmpCacheFilePath); outputProcessor.output("Cache downloaded\n"); } catch (Exception e) { outputProcessor.output("Error download cache: " + e.getMessage() + "\n"); } finally { try { FileUtils.deleteDirectory(tmpDir); } catch (IOException e) { logger.error("", e); } } }
public Image getProfileImage(@Named("id") long id, @Named("imageName")String imageName){ try{ ProfileRecord pr = get(id); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); GoogleCredential credential = GoogleCredential.getApplicationDefault(); Storage.Builder storageBuilder = new Storage.Builder(httpTransport,new JacksonFactory(),credential); Storage storage = storageBuilder.build(); Storage.Objects.Get getObject = storage.objects().get("mybucket", imageName); ByteArrayOutputStream out = new ByteArrayOutputStream(); // If you're not in AppEngine, download the whole thing in one request, if possible. getObject.getMediaHttpDownloader().setDirectDownloadEnabled(false); getObject.executeMediaAndDownloadTo(out); byte[] oldImageData = out.toByteArray(); out.close(); return ImagesServiceFactory.makeImage(oldImageData); }catch(Exception e){ logger.info("Error getting image named "+imageName); } return null; }
Storage.Objects.Get getObject = client.objects().get(bucketName, objectname); getObject.getMediaHttpDownloader().setDirectDownloadEnabled(true); getObject.executeMediaAndDownloadTo(System.out);
@Override public byte[] load(StorageObject from, Map<Option, ?> options) { try { Storage.Objects.Get getRequest = storage.objects() .get(from.getBucket(), from.getName()) .setGeneration(from.getGeneration()) .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)); ByteArrayOutputStream out = new ByteArrayOutputStream(); getRequest.getMediaHttpDownloader().setDirectDownloadEnabled(true); getRequest.executeMediaAndDownloadTo(out); return out.toByteArray(); } catch (IOException ex) { throw translate(ex); } }
/** * Retrieve the whole file (to a temporary file on disk). * * @throws IOException */ public File getAsFile(String bucket, String fname) throws IOException { Storage.Objects.Get request = storage.objects().get(bucket, fname); File file = File.createTempFile("gcsdownload", "obj"); try (OutputStream out = new FileOutputStream(file)) { request.executeMediaAndDownloadTo(out); } return file; }
/** * Retrieve the whole file (to a temporary file on disk). * * @throws IOException */ public File getAsFile(String bucket, String fname) throws IOException { Storage.Objects.Get request = storage.objects().get(bucket, fname); File file = File.createTempFile("gcsdownload", "obj"); try (OutputStream out = new FileOutputStream(file)) { request.executeMediaAndDownloadTo(out); } return file; }
Storage.Objects.Get getObject = storage.objects().get("bucket", "object"); OutputStream out = new FileOutputStream("/tmp/object"); // If you're not in AppEngine, download the whole thing in one request, if possible. getObject.getMediaHttpDownloader().setDirectDownloadEnabled(true); getObject.executeMediaAndDownloadTo(out);
/** * 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 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); }