Description
In which file did you encounter the issue?
Did you change the file? If so, how?
public static void uploadFile(String bucketName, String targetPath, String filePath) throws Exception {
Storage storage = getStorage();
StorageObject object = new StorageObject();
object.setBucket(bucketName);
File file = new File(filePath);
InputStream stream = new FileInputStream(file);
try {
// String contentType =
// URLConnection.guessContentTypeFromStream(stream);
InputStreamContent content = new InputStreamContent("image/jpeg", stream);
Storage.Objects.Insert insert = storage.objects().insert(bucketName, null, content);
insert.setName(targetPath + file.getName());
insert.execute();
} finally {
stream.close();
}
}
public static void uploadFile(String name, String targetPath, String contentType, File file, String bucketName)
throws IOException, GeneralSecurityException, Exception {
InputStreamContent contentStream = new InputStreamContent(contentType, new FileInputStream(file));
contentStream.setLength(file.length());
StorageObject objectMetadata = new StorageObject().setName(targetPath + name)
.setAcl(Arrays.asList(new ObjectAccessControl().setEntity("allUsers").setRole("READER")));
Storage client = getStorage();
Storage.Objects.Insert insertRequest = client.objects().insert(bucketName, objectMetadata, contentStream);
insertRequest.execute();
}
private static Storage getStorage() throws Exception {
if (storage == null) {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
List<String> scopes = new ArrayList<String>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
// Collection<String> scopes = StorageScopes.all();
Credential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory).setServiceAccountId(getProperties().getProperty(ACCOUNT_ID_PROPERTY))
.setServiceAccountPrivateKeyFromP12File(
new File(getProperties().getProperty(PRIVATE_KEY_PATH_PROPERTY)))
.setServiceAccountScopes(scopes).build();
storage = new Storage.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(getProperties().getProperty(APPLICATION_NAME_PROPERTY)).build();
}
return storage;
}
public static void main(String[] args) throws Exception {
CloudStorage.uploadFile("hng-static", "temp/", "/Users/rupanjan/Downloads/15676285_10158033346085311_1317913818452680683_o.jpg");
CloudStorage.uploadFile("15676285_10158033346085311_1317913818452680683_o.jpg", "temp/", "image/jpeg", new File("/Users/rupanjan/Downloads/15676285_10158033346085311_1317913818452680683_o.jpg"),
"hng-static");
}
Describe the issue
I am able to upload the file successfully, but whenever, I click on that "Public Link", it downloads automatically. My intention was to share it for all user with read access. N.B. If I am uploading the file manually from browser, I am able to open the file in browser, but when I upload it programically, it downloads everytime I click on "Public Link"
Please correct me if I am missing anything!!