2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Cloud Storage Client Libraryを使ってGCS上のファイルをReadする

Posted at

Cloud Storage Client Libraryを使ってGCS上のファイル内容を取得する際のメモ。
以下の例だとバケットを作る例しかなくて、Readする例がなかったのでAPIマニュアル見ながら作成。
https://cloud.google.com/storage/docs/reference/libraries

以下はGCS上に置いたAWSのクレデンシャル情報を読み込む例。

//Storageオブジェクトの生成
Storage storage = StorageOptions.getDefaultInstance().getService();

//Bucket名を設定
String bucketName = "{BUKET名}";

//オブジェクト名を設定
String blobName = "{ファイル名}";

//Bucket名とオブジェクト名を元にBlobIdを取得
BlobId blobId = BlobId.of(bucketName, blobName);

//Blobオブジェクトを生成
Blob blob = storage.get(blobId);

//Blobオブジェクトの内容を取得
byte[] content = blob.getContent(BlobSourceOption.generationMatch());

//CSV形式で記載してるのでsplitして各変数に格納
String credential[] = new String(content, "UTF-8").split(",", 0);
aws_key = credential[0];
aws_secret_key = credential[1];
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?