Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 谷歌云存储中带有其密钥的图像URL_Java_Google Cloud Storage - Fatal编程技术网

Java 谷歌云存储中带有其密钥的图像URL

Java 谷歌云存储中带有其密钥的图像URL,java,google-cloud-storage,Java,Google Cloud Storage,我实际上有一个问题,我想知道如何在谷歌云存储中用他的密钥恢复图像URL,下面是代码 StorageUtils.java public class StorageUtils { public static Storage storage; private static final String DATETIME_FORMAT = "yyyyMMdd_HHmmss"; private static final String CAMERA_FILENAME_PREFIX = "IMG_"; /**

我实际上有一个问题,我想知道如何在谷歌云存储中用他的密钥恢复图像URL,下面是代码

StorageUtils.java

public class StorageUtils {
public static Storage storage;
private static final String DATETIME_FORMAT = "yyyyMMdd_HHmmss";
private static final String CAMERA_FILENAME_PREFIX = "IMG_";

/**
 * Uploads a file to a bucket. Filename and content type will be based on
 * the original file
 * @param bucketName
 *            Bucket where file will be uploaded
 * @param filePath
 *            Absolute path of the file to upload
 * @throws Exception
 */
public static void  uploadFile(String bucketName, String filePath)throws Exception {
    Storage storage = getStorage();
    String timeStamp = new SimpleDateFormat(DATETIME_FORMAT).format(new Date());
    String imageFileName = CAMERA_FILENAME_PREFIX + timeStamp;
    StorageObject object = new StorageObject();
    object.setBucket(bucketName);

    File file = new File(filePath);

    InputStream stream = new FileInputStream(file);
    try {
        String contentType = URLConnection
                .guessContentTypeFromStream(stream);
        InputStreamContent content = new InputStreamContent(contentType,
                stream);
        Storage.Objects.Insert insert = storage.objects().insert(
                bucketName, null, content);
        insert.setName(imageFileName + file.getName());
        insert.execute();
        /////

        /////
    } finally {
        stream.close();
    }

}

public static void downloadFile(String bucketName, String fileName, String destinationDirectory) throws Exception {

    File directory = new File(destinationDirectory);
    if(!directory.isDirectory()) {
        throw new Exception("Provided destinationDirectory path is not a directory");
    }
    File file = new File(directory.getAbsolutePath() + "/" + fileName);

    Storage storage = getStorage();

    Storage.Objects.Get get = storage.objects().get(bucketName, fileName);
    FileOutputStream stream = new FileOutputStream(file);
    try{
        get.executeMediaAndDownloadTo(stream);
    } finally {
        stream.close();
    }
}

/**
 * Deletes a file within a bucket
 *
 * @param bucketName
 *            Name of bucket that contains the file
 * @param fileName
 *            The file to delete
 * @throws Exception
 */
public static void deleteFile(String bucketName, String fileName) throws Exception {
    Storage storage = getStorage();
    storage.objects().delete(bucketName, fileName).execute();
}

/**
 * Creates a bucket
 *
 * @param bucketName
 *            Name of bucket to create
 * @throws Exception
 */
public static void createBucket(String bucketName) throws Exception {
    Storage storage = getStorage();
    Bucket bucket = new Bucket();
    bucket.setName(bucketName);
    storage.buckets().insert(StorageConstants.PROJECT_ID_PROPERTY, bucket).execute();
}

/**
 * Deletes a bucket
 *
 * @param bucketName
 *            Name of bucket to delete
 * @throws Exception
 */
public static void deleteBucket(String bucketName) throws Exception {
    Storage storage = getStorage();
    storage.buckets().delete(bucketName).execute();
}

/**
 * Lists the objects in a bucket
 *
 * @param bucketName bucket name to list
 * @return Array of object names
 * @throws Exception
 */
public static List<String> listBucket(String bucketName) throws Exception {

    Storage storage = getStorage();

    List<String> list = new ArrayList<String>();

    List<StorageObject> objects = storage.objects().list(bucketName).execute().getItems();
    if(objects != null) {
        for(StorageObject o : objects) {
            list.add(o.getName());
        }
    }
    return list;
}

/**
 * List the buckets with the project
 * (Project is configured in properties)
 *
 * @return
 * @throws Exception
 */
public static List<String> listBuckets() throws Exception {
    Storage storage = getStorage();
    List<String> list = new ArrayList<String>();
    List<Bucket> buckets = storage.buckets().list(StorageConstants.PROJECT_ID_PROPERTY).execute().getItems();
    if(buckets != null) {
        for(Bucket b : buckets) {
            list.add(b.getName());
        }
    }

    return list;
}

private static Storage getStorage() throws Exception {
    if (storage == null) {

        ApacheHttpTransport httpTransport = new ApacheHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        List<String> scopes = new ArrayList<String>();
        scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);

        Credential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId(StorageConstants.ACCOUNT_ID_PROPERTY)
                .setServiceAccountPrivateKeyFromP12File(getTempPkc12File())
                .setServiceAccountScopes(scopes).build();

        storage = new Storage.Builder(httpTransport, jsonFactory,
                credential).setApplicationName(StorageConstants.APPLICATION_NAME_PROPERTY)
                .build();
    }

    return storage;
}

private static File getTempPkc12File() throws IOException {
    InputStream pkc12Stream = StorageConstants.CONTEXT.getAssets().open("key.p12");
    File tempPkc12File = File.createTempFile("temp_pkc12_file", "p12");
    OutputStream tempFileStream = new FileOutputStream(tempPkc12File);

    int read = 0;
    byte[] bytes = new byte[1024];
    while ((read = pkc12Stream.read(bytes)) != -1) {
        tempFileStream.write(bytes, 0, read);
    }
    return tempPkc12File;
}

}

请告诉我们您遇到了什么错误。事实上,我不知道如何在云存储中检索图像上传的密钥和url谢谢您是否希望url为您上传到某个存储桶和对象的对象提供服务?最简单的URL是。请注意,该对象必须是公开可读的,才能正常工作。您好,请原谅我在“完成”中没有非常清楚,我希望在发送图像后返回对图像URL及其密钥的响应,以便您可以将它们与其他for data一起存储在数据存储中。再次感谢你。