Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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从azure blob获取特定文件的属性?_Azure_Azure Storage Blobs_Azure Blob Storage - Fatal编程技术网

如何使用Java从azure blob获取特定文件的属性?

如何使用Java从azure blob获取特定文件的属性?,azure,azure-storage-blobs,azure-blob-storage,Azure,Azure Storage Blobs,Azure Blob Storage,我正在使用Java测试blob中的项目。我已经有一个文件列表,我希望在blob中出现。下面是我对maven的依赖 <dependency> <groupId>com.microsoft.azure</groupId> <artifactId>azure-storage</artifactId> <version>1.2.0</version> </dependency> c

我正在使用Java测试blob中的项目。我已经有一个文件列表,我希望在blob中出现。下面是我对maven的依赖

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-storage</artifactId>
    <version>1.2.0</version>
</dependency>

com.microsoft.azure


请帮我提供一个代码示例,演示如何下载特定文件。

好的,我得到了一个如下所示的方法,可以让它正常工作。这方面没有直接的实现。您首先必须导航到该目录并检索具有确切名称的文件

/**
 * This method will help you retrieve properties of a file or a list of files from particular folder in Azure Blob storage
 * @param containerName - Pass the name of the container
 * @param path - Location of your file
 * @param fileName - You can pass complete file name to retrieve one file or you can pass prefix of the file.
 * @return It returns List<BlobProperties> if you pass complete file name, it will return one file or it will find file with supplied prefix.
 * @throws Exception
 */

public List<BlobProperties> retriveBlobFilesProperties(String containerName, String path, String fileName) throws Exception{
    List<BlobProperties> blobFilesProperties = new ArrayList<BlobProperties>();
    CloudStorageAccount cloudStorageAccount = CloudStorageAccount.parse(this.storageConnectionString);
    CloudBlobClient cloudBlobClient = cloudStorageAccount.createCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = cloudBlobClient.getContainerReference(containerName);
    CloudBlobDirectory directory = cloudBlobContainer.getDirectoryReference(path);
    Iterable<ListBlobItem> blobItems = directory.listBlobs(fileName);
    for(ListBlobItem item : blobItems){
        CloudBlob blob = (CloudBlob)item;
        blobFilesProperties.add(blob.getProperties());
    }
    return blobFilesProperties;
}
/**
*此方法将帮助您从Azure Blob存储中的特定文件夹中检索文件或文件列表的属性
*@param containerName-传递容器的名称
*@param path-文件的位置
*@param fileName-您可以传递完整的文件名以检索一个文件,也可以传递文件的前缀。
*@return返回列表如果您传递完整的文件名,它将返回一个文件或找到带有提供前缀的文件。
*@抛出异常
*/
公共列表检索BlobFileProperties(字符串containerName、字符串路径、字符串文件名)引发异常{
List blobFilesProperties=new ArrayList();
CloudStorageAccount CloudStorageAccount=CloudStorageAccount.parse(this.storageConnectionString);
CloudBlobClient CloudBlobClient=cloudStorageAccount.createCloudBlobClient();
CloudBlobContainer CloudBlobContainer=cloudBlobClient.getContainerReference(containerName);
CloudBlobDirectoryDirectory=cloudBlobContainer.getDirectoryReference(路径);
Iterable blobItems=directory.listBlobs(文件名);
对于(ListBlobItem项:blobItems){
CloudBlob blob=(CloudBlob)项;
添加(blob.getProperties());
}
返回BlobFile属性;
}

src:

此代码使您能够从azure blob容器中检索特定文件

使用下面的azure存储版本

使用以下方法从azure下载特定文件 blob容器并将文件作为字节数组返回


上面的代码将只获取blob的属性。我以为你想下载。不。我只是想得到这个斑点的属性。谢谢你的指点。啊,我明白了。从题名中,我以为你想下载blob,因此评论:)。有没有一种更优雅的方法可以做到这一点而不盲目地使用它们?
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-storage</artifactId>
    <version>8.6.0</version>
</dependency>
package com.azure.config;

import java.net.URISyntaxException;
import java.security.InvalidKeyException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.blob.CloudBlobClient;

@Configuration
public class AzureBlobConfiguration {


    /**
     * Cloud blob client.
     *
     * @return the cloud blob client
     * @throws URISyntaxException the URI syntax exception
     * @throws InvalidKeyException the invalid key exception
     */
    @Bean
    public CloudBlobClient cloudBlobClient()throws URISyntaxException, InvalidKeyException {
        CloudStorageAccount storageAccount = CloudStorageAccount.parse("Blob-Connection-String-Value");
        return storageAccount.createCloudBlobClient();
    }

}

        
package com.azure.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;

import org.springframework.beans.factory.annotation.Autowired;

import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlockBlob;

public class AzureBlob {

    @Autowired
    private CloudBlobClient cloudBlobClient;
    
    private byte[] downloadFile(String containerName, String filename)throws URISyntaxException, StorageException, IOException {
    
        CloudBlobContainer container = cloudBlobClient.getContainerReference(containerName);
        
        CloudBlockBlob cloudBlockBlob = container.getBlockBlobReference(filename);
    
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            cloudBlockBlob.download(outputStream);
            return outputStream.toByteArray();
        }   
    }
}