谷歌云存储的java中getServingUrl方法出现异常

问题描述 投票:0回答:1

Java类文件如下

public class GoogleCloudStorageTest {

    static String bucketName = "test-bucket";
    static String blobName = "ImageName.JPG";
    public static void main(String... args) throws Exception {
        String fileName = "E:\\ImageName.JPG";
        String jsonPath = "E:\\Json_File.json";

        Credentials credentials = GoogleCredentials. fromStream(new FileInputStream(jsonPath));

        Storage storage = StorageOptions.newBuilder().setCredentials(credentials)
                  .setProjectId("xxx-xxx").build().getService();

        Path imageFile = Paths.get(fileName);

        uploadFileToGoogleCloud(storage, imageFile);
        String servingUrl = servingurl(java.nio.file.Files.readAllBytes(imageFile));
        System.out.println(servingUrl);
    }

    //method to upload file to google cloud
    public static String uploadFileToGoogleCloud(Storage storage, Path zipFile) throws IOException {

        Hasher hasher = Hashing.crc32c().newHasher();
        byte[] bufferHash = new byte[1024];
        try(InputStream input = Files.newInputStream(zipFile)){
            int limit;
            while((limit = input.read(bufferHash))>=0) {
                hasher.putBytes(ByteBuffer.wrap(bufferHash,0,limit));

            }
        }
        BlobInfo blob = BlobInfo.newBuilder(bucketName,blobName).setContentType("APPLICATION/OCTET-STREAM")
                .setCrc32c(BaseEncoding.base64().encode(Ints.toByteArray(hasher.hash().asInt())))
                 .setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Acl.Role.READER))))
                .build();

        try(WriteChannel writer = storage.writer(blob,Storage.BlobWriteOption.crc32cMatch())){
            byte[] buffer = new byte[1024];
            try (InputStream input = Files.newInputStream(zipFile)) {
                int limit;
                while((limit = input.read(buffer))>=0) {
                    try {
                        writer.write(ByteBuffer.wrap(buffer, 0, limit));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            writer.close();
        }
        return storage.get(bucketName).get(blobName).getMediaLink();

        }


    **public static String servingurl(byte[] imageBytes) throws IOException{
        ImagesService images = ImagesServiceFactory.getImagesService();
        ServingUrlOptions options = ServingUrlOptions.Builder
                .withGoogleStorageFileName("/gs/" + bucketName + "/"+blobName)
                .imageSize(150)
                .crop(true)
                .secureUrl(true);

        String url = images.getServingUrl(options);   //getting error at this line
        return url;
    }**
}

这是异常堆栈跟踪

com.google.apphosting.api.ApiProxy$CallNotFoundException: The API package 'blobstore' or call 'CreateEncodedGoogleStorageKey()' was not found.
    at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:109)
    at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:64)
    at com.google.appengine.api.blobstore.BlobstoreServiceImpl.createGsBlobKey(BlobstoreServiceImpl.java:304)
    at com.google.appengine.api.images.ImagesServiceImpl.getServingUrl(ImagesServiceImpl.java:266)
    at com.xxx.cloudStorage.GoogleCloudStorageTest.resizeImage(GoogleCloudStorageTest.java:100)
    at com.xxx.cloudStorage.GoogleCloudStorageTest.main(GoogleCloudStorageTest.java:51)

在这里,我能够上传图像文件到谷歌云,但我无法得到服务的网址,任何人都请让我知道,任何配置丢失?

java google-app-engine google-cloud-storage blobstore
1个回答
0
投票

该错误似乎表明没有调用创建存储密钥。试试这个。

String key = ""; // path to the image "/gs/mybucket/myfile.jpg"
ImagesService images = ImagesServiceFactory.getImagesService();
BlobstoreService blobstore = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKey = blobstore.createGsBlobKey(key);
ServingUrlOptions opts = ServingUrlOptions.Builder
        .withBlobKey(blobKey)
        .imageSize(150)
        .crop(true)
        .secureUrl(true);
© www.soinside.com 2019 - 2024. All rights reserved.