Created
May 4, 2015 22:59
-
-
Save ibalashov/a9748fee544fdb3cb4d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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