Download 正在获取文件gcs的base 64字符串

Download 正在获取文件gcs的base 64字符串,download,base64,blob,google-cloud-storage,Download,Base64,Blob,Google Cloud Storage,我想知道firebase(gcs)是否提供了下载文件base64字符串的方法。我想用java将其作为API响应传递 无论我搜索了什么,我都发现地面军事系统在回复blob。我想将此blob转换为base64字符串。但是,import com.google.cloud.storage.Blob不支持 blob.getBytes() 我们将非常感谢您的帮助 您可以使用以下代码下载Google云存储文件的内容,然后将该内容编码到Base64: package com.example.storage;

我想知道firebase(gcs)是否提供了下载文件base64字符串的方法。我想用java将其作为API响应传递

无论我搜索了什么,我都发现地面军事系统在回复blob。我想将此blob转换为base64字符串。但是,
import com.google.cloud.storage.Blob
不支持

blob.getBytes()

我们将非常感谢您的帮助

您可以使用以下代码下载Google云存储文件的内容,然后将该内容编码到Base64:

package com.example.storage;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.Blob.BlobSourceOption;
import java.util.Arrays; 
import org.apache.commons.codec.binary.Base64;

public class EncodingSample {
  public static void main(String... args) throws Exception {
    // Instantiates a client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    Blob blob = storage.get(BlobId.of("your-bucket", "your-object"));
    System.out.printf("Data: %s %n", blob.toString());
    // Get object content
    byte[] content = blob.getContent(BlobSourceOption.generationMatch());
    // byte array to string
    String s = new String(content);
    // Bytes values
    System.out.printf("Array: %s %n", Arrays.toString(content));
    // Object content in string
    System.out.printf("Text: %s %n", s);
    // String encoded to Base64
    byte[] encodedBytes = Base64.encodeBase64(s.getBytes());
    // Base64 bytes
    System.out.printf("encodedBytes: %s %n", Arrays.toString(encodedBytes));
    // Base64 encoded string
    System.out.println("String encoded: " + new String(encodedBytes));
  }
}
有关
getContent
方法的更多信息,请遵循以下步骤


希望有帮助

您可以使用以下代码下载Google云存储文件的内容,然后将该内容编码到Base64:

package com.example.storage;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.Blob.BlobSourceOption;
import java.util.Arrays; 
import org.apache.commons.codec.binary.Base64;

public class EncodingSample {
  public static void main(String... args) throws Exception {
    // Instantiates a client
    Storage storage = StorageOptions.getDefaultInstance().getService();
    Blob blob = storage.get(BlobId.of("your-bucket", "your-object"));
    System.out.printf("Data: %s %n", blob.toString());
    // Get object content
    byte[] content = blob.getContent(BlobSourceOption.generationMatch());
    // byte array to string
    String s = new String(content);
    // Bytes values
    System.out.printf("Array: %s %n", Arrays.toString(content));
    // Object content in string
    System.out.printf("Text: %s %n", s);
    // String encoded to Base64
    byte[] encodedBytes = Base64.encodeBase64(s.getBytes());
    // Base64 bytes
    System.out.printf("encodedBytes: %s %n", Arrays.toString(encodedBytes));
    // Base64 encoded string
    System.out.println("String encoded: " + new String(encodedBytes));
  }
}
有关
getContent
方法的更多信息,请遵循以下步骤

希望能有帮助