使用Java

时间:2017-09-14 14:39:45

标签: java google-cloud-storage

我想使用java服务器将文件添加到我的Google云端存储(实际上是Firebase存储桶)。

我使用的是我上网的代码:

    // Authenticate using a service account
    Storage storage;
    try {
        storage = StorageOptions.newBuilder()
                .setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream("C:/tmp/firebasekey.json")))
                .build()
                .getService();
        // The name for the new bucket
        String bucketName = "MinhaSaude";

        // Creates the new bucket
        Bucket bucket = storage.create(BucketInfo.of(bucketName));
        // Create blob
        BlobId blobId = BlobId.of("bucket", "blob_name");
        // Add metadata to the blob
        BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
        // Upload blob to GCS (same as Firebase Storage)
        Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes());

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我收到的是Bucket bucket = storage.create(BucketInfo.of(bucketName))后的错误;

“无效的存储桶名称:'MinhaSaude'” }

或“存储桶帐户”minhasaude-api \“已被停用。”

我找不到满足谷歌的水桶名称。我已经使用浏览器将文件上传到Firebase并上传了。

2 个答案:

答案 0 :(得分:1)

您之前创建的存储桶是否可能存在?尝试随机字符串,看看是否有效。

或者,只有在为GCS项目启用计费后,才能创建GCS存储桶。您是否使用信用卡和诸如此类的东西设置项目?

答案 1 :(得分:1)

尝试我的代码,它对我有用

String filename =  httpRequest.getFirstQueryParameter("name").get();
    String contentType =  httpRequest.getFirstQueryParameter("content-type").get();

    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("key.json").getFile());
    FileInputStream serviceAccount = new FileInputStream(file);

    FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(GoogleCredentials.fromStream(serviceAccount))
            .setStorageBucket("<you bucket name>")
            .build();

    FirebaseApp firebaseApp = FirebaseApp.initializeApp(options);
    Bucket bucket = StorageClient.getInstance(firebaseApp).bucket();


    Storage storage = StorageOptions.newBuilder().setProjectId("firebaseId")
            .setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(file)))
            .build()
            .getService();
    file = new File(classLoader.getResource("jar-loading.gif").getFile());

    InputStream inputStream = new FileInputStream(file);
    BlobInfo blobInfo = BlobInfo.newBuilder(bucket.getName(), filename)
            .setContentType(contentType).build();

    try (WriteChannel writer = storage.writer(blobInfo)) {
        byte[] buffer = new byte[1024];
        int limit;
        try {
            while ((limit = inputStream.read(buffer)) >= 0) {
                writer.write(ByteBuffer.wrap(buffer, 0, limit));
            }

        } catch (Exception ex) {
            // handle exception
        } finally {
            writer.close();
        }
    }

使用该代码,我可以将gif文件从我的项目上传到Firebase存储。进行更改以使用它