Skip to content

Instantly share code, notes, and snippets.

@ibalashov
Created May 4, 2015 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibalashov/a9748fee544fdb3cb4d4 to your computer and use it in GitHub Desktop.
Save ibalashov/a9748fee544fdb3cb4d4 to your computer and use it in GitHub Desktop.
public StorageObject uploadFile(String bucketName, File file, String remoteDirectory) throws Exception {
InputStream stream = new BufferedInputStream(new FileInputStream(file));
try {
InputStreamContent mediaContent = new InputStreamContent("application/octet-stream", stream);
mediaContent.setLength(file.length());
Storage.Objects.Insert insert = storage.objects().insert(bucketName, null, mediaContent);
insert.getMediaHttpUploader().setDisableGZipContent(true);
if (mediaContent.getLength() > 0 && mediaContent.getLength() <= 2 * 1000 * 1000 /* 2MB */) {
insert.getMediaHttpUploader().setDirectUploadEnabled(true);
}
insert.setName(Joiner.on('/').join(remoteDirectory, file.getName()));
return insert.execute();
} finally {
stream.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment