get.getMediaHttpDownloader().setDirectDownloadEnabled(true); try (OutputStream os = new FileOutputStream(localPath.toFile()); InputStream is = get.executeMediaAsInputStream()) {
getObject.getMediaHttpDownloader().setDirectDownloadEnabled(true); getObject.executeMediaAndDownloadTo(out);
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; }
/** * 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 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); } }
Storage.Objects.Get getObject = client.objects().get(bucketName, objectname); getObject.getMediaHttpDownloader().setDirectDownloadEnabled(true); getObject.executeMediaAndDownloadTo(System.out);
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 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 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); }
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(); }