Google Storage ::点击"公共链接"下载文件

时间:2017-05-10 12:46:22

标签: java google-cloud-storage

我指的是Github链接,

https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/storage/json-api/src/main/java/StorageSample.java

代码段

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.createBucket("my-bucket3/good");

        CloudStorage.uploadFile("my-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"),
                "my-static");

        // CloudStorage.downloadFile("my-bucket", "some-file.jpg",
        // "/var/downloads");

        List<String> buckets = CloudStorage.listBuckets();
        System.out.println(buckets.size());
    }

我面临的问题,

我能够成功上传文件,但无论何时,我点击了#34;公共链接&#34;,它会自动下载。我的目的是为所有具有读访问权限的用户共享它。 注:如果我从浏览器手动上传文件,我可以在浏览器中打开文件,但是当我以编程方式上传文件时,每次点击&#34;公共链接&#34;

如果我错过任何东西,请纠正我!

1 个答案:

答案 0 :(得分:0)

听起来您知道存储桶和对象的名称,并且这些对象都是公开可读的,并且您需要一个可以共享的URL,以允许其他人读取该对象。

为此,无需在控制台中使用“公共链接”功能。公共URL可以通过编程方式构建。他们遵循这种模式:

https://storage.googleapis.com/bucket_name/object_name