2017-09-14 65 views
0

我想使用Java服务器将文件添加到我的Google Cloud Storage(实际上是Firebase存储桶)。使用Java将文件上传到Fire Storage for 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(); 
    } 

我收到后面是后桶桶= storage.create(BucketInfo.of(bucketName))误差修改;

无论是 “无效的分区名称: 'MinhaSaude'” }

或minhasaude-API \ “的铲斗\帐户”, “已禁用”。

我无法找到一个满足谷歌的bucketname。我已经使用浏览器将文件上传到Firebase,并且其上传正常。

+0

你能链接到你在哪里找到你的代码吗? –

+0

我删除了Firebase的标签,因为这纯粹是Cloud Storage SDK代码。 –

回答

0

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

或者,只有在为GCS项目启用结算功能后,才能创建GCS存储桶。你有没有使用信用卡设置你的项目?

相关问题