欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AWS S3上传文件、下载文件、删除。

程序员文章站 2023-12-30 17:57:10
...

最近遇到一个『文件上传』的需求,要求从亚马逊的S3迁移为Azure Storage(微软的存储服务)。 

废话不多,直接上示例(不过要注意一个问题,你的服务链接是在国内还是国外):

maven依赖:

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

java类:


package com.customerservicePES.AzureStorage;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.EnumSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.customerservicePES.utils.SystemConfigUtils;
import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.OperationContext;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.BlobContainerPermissions;
import com.microsoft.azure.storage.blob.BlobContainerPublicAccessType;
import com.microsoft.azure.storage.blob.BlobRequestOptions;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlockBlob;
import com.microsoft.azure.storage.blob.SharedAccessBlobPermissions;
import com.microsoft.azure.storage.blob.SharedAccessBlobPolicy;

/**
 * 
 * @author zhangjiamei
 *
 */
public class AzureStorageV7Util {
    private static Logger logger = LoggerFactory.getLogger(AzureStorageV7Util.class);

    /* *************************************************************************************************************************
     * Instructions: Update the storageConnectionString variable with your AccountName and Key and then run the sample.
     * *************************************************************************************************************************
     */
    public static final String storageConnectionString =
            "DefaultEndpointsProtocol=https;" +
                    "AccountName=%s;" +
                    "AccountKey=%s;EndpointSuffix=core.chinacloudapi.cn";

    public static  CloudStorageAccount storageAccount = null;
    public static  CloudBlobClient blobClient = null;
    public static  CloudBlobContainer container=null;
    

    public static String uploadInputStreamToAzureStorage(InputStream inputStream, String fileNameALl)
    {
        String key = null;
        File sourceFile = null;
        try {
            // Parse the connection string and create a blob client to interact with Blob storage
            if(storageAccount == null){
                storageAccount = CloudStorageAccount.parse(String.format(storageConnectionString, SystemConfigUtils.getAzureStorageAccountName(),SystemConfigUtils.getAzureStorageAccountKey()));
            }else{
                logger.info("storageAccount is not null!!!");
            }
            if(blobClient == null){
                blobClient = storageAccount.createCloudBlobClient();
            }else{
                logger.info("blobClient is not null!!!");
            }
            if(container == null){
                container = blobClient.getContainerReference(SystemConfigUtils.getAzureStoragecontainer());
            }else{
                logger.info("container is not null!!!");
            }

            // Create the container if it does not exist with public access.
            logger.info("Creating container: " + container.getName());
            container.createIfNotExists(BlobContainerPublicAccessType.CONTAINER, new BlobRequestOptions(), new OperationContext());            

            logger.info(fileNameALl.substring(0, fileNameALl.lastIndexOf(".")));
            logger.info(fileNameALl.substring(fileNameALl.lastIndexOf(".")+1, fileNameALl.length()));

            //Creating a sample file  创建文件
            if(fileNameALl.substring(0, fileNameALl.lastIndexOf(".")).length() >= 3){
                sourceFile = createTempFile(inputStream, fileNameALl.substring(0, fileNameALl.lastIndexOf(".")), fileNameALl.substring(fileNameALl.lastIndexOf(".")+1, fileNameALl.length()));
            }else{
                sourceFile = createTempFile(inputStream, fileNameALl.substring(0, fileNameALl.lastIndexOf("."))+"__", fileNameALl.substring(fileNameALl.lastIndexOf(".")+1, fileNameALl.length()));
            }
            logger.info("Creating a sample file at: " + sourceFile.toString());

            //Getting a blob reference
            CloudBlockBlob blob = container.getBlockBlobReference(sourceFile.getName());

            //Creating blob and uploading file to it
            logger.info("file name : "+sourceFile.getName());
            logger.info("Uploading the sample file ");
            blob.uploadFromFile(sourceFile.getAbsolutePath());

            key = sourceFile.getName();//Azure Storage Blob 使用上传文件名称作为『唯一ID』。下载时使用。
            //Delete tempFile "sourceFile"
            if(sourceFile.exists())
                sourceFile.delete();

            logger.info("Deleted tempFile sourceFile!!");

        } 
        catch (StorageException ex)
        {
            logger.info(String.format("Error returned from the service. Http code: %d and error code: %s", ex.getHttpStatusCode(), ex.getErrorCode()));
            logger.error(ex.getMessage(), ex);
        }
        catch (Exception ex) 
        {
            logger.error(ex.getMessage(), ex);
        }
        finally 
        {
            logger.info("over");
        }
        return key;
    }
    static File createTempFile(InputStream inputStream, String fileName, String suffix) throws IOException {
        File sampleFile = null;
        sampleFile = File.createTempFile(fileName, "."+suffix);
        logger.info(">> Creating a sample file at: " + sampleFile.toString());

        byte[] b = new byte[1024];
        int n = 0;
        File targetFile = new File(sampleFile.getAbsolutePath());
        if(targetFile.exists()){
            logger.info("目标文件 targetFile 存在。");
        }
        //写入临时文件
        BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(targetFile));//使用『缓存输出流』进行写入
        while ((n = inputStream.read(b)) != -1) {
            output.write(b, 0, n);
        }
        output.close();
        return targetFile;
    }
    
    public static Map<String, String> uploadInputStreamToAzureStorageAndGetUrl(InputStream inputStream, String fileNameALl)
    {
        Map<String, String> map = new HashMap<String, String>();
        File sourceFile = null;
        try {
            // Parse the connection string and create a blob client to interact with Blob storage
            if(storageAccount == null){
                storageAccount = CloudStorageAccount.parse(String.format(storageConnectionString, SystemConfigUtils.getAzureStorageAccountName(),SystemConfigUtils.getAzureStorageAccountKey()));
            }else{
                logger.info("storageAccount is not null!!!");
            }
            if(blobClient == null){
                blobClient = storageAccount.createCloudBlobClient();
            }else{
                logger.info("blobClient is not null!!!");
            }
            if(container == null){
                container = blobClient.getContainerReference(SystemConfigUtils.getAzureStoragecontainer());
            }else{
                logger.info("container is not null!!!");
            }

            // Create the container if it does not exist with public access.
            logger.info("Creating container: " + container.getName());
            container.createIfNotExists(BlobContainerPublicAccessType.CONTAINER, new BlobRequestOptions(), new OperationContext());            

            logger.info(fileNameALl.substring(0, fileNameALl.lastIndexOf(".")));
            logger.info(fileNameALl.substring(fileNameALl.lastIndexOf(".")+1, fileNameALl.length()));

            //Creating a sample file  创建文件
            if(fileNameALl.substring(0, fileNameALl.lastIndexOf(".")).length() >= 3){
                sourceFile = createTempFile(inputStream, fileNameALl.substring(0, fileNameALl.lastIndexOf(".")), fileNameALl.substring(fileNameALl.lastIndexOf(".")+1, fileNameALl.length()));
            }else{
                sourceFile = createTempFile(inputStream, fileNameALl.substring(0, fileNameALl.lastIndexOf("."))+"__", fileNameALl.substring(fileNameALl.lastIndexOf(".")+1, fileNameALl.length()));
            }
            logger.info("Creating a sample file at: " + sourceFile.toString());

            //Getting a blob reference
            CloudBlockBlob blob = container.getBlockBlobReference(sourceFile.getName());

            //Creating blob and uploading file to it
            logger.info("file name : "+sourceFile.getName());
            logger.info("Uploading the sample file ");
            blob.uploadFromFile(sourceFile.getAbsolutePath());
            //Delete tempFile "sourceFile"
            if(sourceFile.exists())
                sourceFile.delete();

            logger.info("Deleted tempFile sourceFile!!");
            
            String key = sourceFile.getName();//Azure Storage Blob 使用上传文件名称作为『唯一ID』。下载时使用。
            map.put("key", sourceFile.getName());
            map.put("url", getBlobUrl(key));
            map.put("fileName", fileNameALl);
        } 
        catch (StorageException ex)
        {
            logger.info(String.format("Error returned from the service. Http code: %d and error code: %s", ex.getHttpStatusCode(), ex.getErrorCode()));
            logger.error(ex.getMessage(), ex);
        }
        catch (Exception ex) 
        {
            logger.error(ex.getMessage(), ex);
        }
        finally 
        {
            logger.info("over");
        }
        return map;
    }

    public static File downloadFromStorage(String fileNameALl){
        File downloadedFile = null;
        try{
            // Parse the connection string and create a blob client to interact with Blob storage
            if(storageAccount == null){
                storageAccount = CloudStorageAccount.parse(String.format(storageConnectionString, SystemConfigUtils.getAzureStorageAccountName(),SystemConfigUtils.getAzureStorageAccountKey()));
            }else{
                logger.info("storageAccount is not null!!!");
            }
            if(blobClient == null){
                blobClient = storageAccount.createCloudBlobClient();
            }else{
                logger.info("blobClient is not null!!!");
            }
            if(container == null){
                container = blobClient.getContainerReference(SystemConfigUtils.getAzureStoragecontainer());
            }else{
                logger.info("container is not null!!!");
            }

            // Create the container if it does not exist with public access.
            logger.info("Creating container: " + container.getName());
            container.createIfNotExists(BlobContainerPublicAccessType.CONTAINER, new BlobRequestOptions(), new OperationContext());          

            logger.info(fileNameALl.substring(0, fileNameALl.lastIndexOf(".")));
            logger.info(fileNameALl.substring(fileNameALl.lastIndexOf("."), fileNameALl.length()));

            // Download blob. In most cases, you would have to retrieve the reference
            // to cloudBlockBlob here. However, we created that reference earlier, and 
            // haven't changed the blob we're interested in, so we can reuse it. 
            // Here we are creating a new file to download to. Alternatively you can also pass in the path as a string into downloadToFile method: blob.downloadToFile("/path/to/new/file").
            downloadedFile = File.createTempFile(fileNameALl.substring(0, fileNameALl.lastIndexOf(".")), fileNameALl.substring(fileNameALl.lastIndexOf("."), fileNameALl.length()));
            //Getting a blob reference
            CloudBlockBlob blob = container.getBlockBlobReference(fileNameALl);
            logger.info("download starting");
            logger.info("downloadedFile.getAbsolutePath() : "+downloadedFile.getAbsolutePath());
            blob.downloadToFile(downloadedFile.getAbsolutePath());
            logger.info("download end");
            return downloadedFile;
        }catch (StorageException ex)
        {
            logger.info(String.format("Error returned from the service. Http code: %d and error code: %s", ex.getHttpStatusCode(), ex.getErrorCode()));
            logger.error(ex.getMessage(), ex);
        }
        catch (Exception ex) 
        {
            logger.error(ex.getMessage(), ex);
        }
        finally 
        {
            logger.info("over");
        }
        return downloadedFile;
    }

    public static String getBlobUrl(String key){
        String url = null;
        try{
            SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            // Immediately applicable
            policy.setSharedAccessStartTime(calendar.getTime());
            // Applicable time span is 100 Year
            calendar.add(Calendar.YEAR, 100);
            policy.setSharedAccessExpiryTime(calendar.getTime());
            // SAS grants READ access privileges
            policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ));
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

            // Parse the connection string and create a blob client to interact with Blob storage
            if(storageAccount == null){
                storageAccount = CloudStorageAccount.parse(String.format(storageConnectionString, SystemConfigUtils.getAzureStorageAccountName(),SystemConfigUtils.getAzureStorageAccountKey()));
            }else{
                logger.info("storageAccount is not null!!!");
            }
            if(blobClient == null){
                blobClient = storageAccount.createCloudBlobClient();
            }else{
                logger.info("blobClient is not null!!!");
            }
            if(container == null){
                container = blobClient.getContainerReference(SystemConfigUtils.getAzureStoragecontainer());
                // Private blob-container with no access for anonymous users
                containerPermissions.setPublicAccess(BlobContainerPublicAccessType.OFF);
                container.uploadPermissions(containerPermissions);
            }else{
                logger.info("container is not null!!!");
            }
            String sas = container.generateSharedAccessSignature(policy,null);
            CloudBlockBlob blob = container.getBlockBlobReference(key);
            String blobUri = blob.getUri().toString();
            url = ( blobUri+"?"+sas);
            logger.info("url : " + url);
            return url;
        }catch (StorageException ex)
        {
            logger.info(String.format("Error returned from the service. Http code: %d and error code: %s", ex.getHttpStatusCode(), ex.getErrorCode()));
            logger.error(ex.getMessage(), ex);
        }
        catch (Exception ex) 
        {
            logger.error(ex.getMessage(), ex);
        }
        finally 
        {
            logger.info("over");
        }
        return null;
    }
    
    public static void deleteByKey(String fileNameAll){
        String url = null;
        try{
            SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            // Immediately applicable
            policy.setSharedAccessStartTime(calendar.getTime());
            // Applicable time span is 100 Year
            calendar.add(Calendar.YEAR, 100);
            policy.setSharedAccessExpiryTime(calendar.getTime());
            // SAS grants READ access privileges
            policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ));
            BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

            // Parse the connection string and create a blob client to interact with Blob storage
            if(storageAccount == null){
                storageAccount = CloudStorageAccount.parse(String.format(storageConnectionString, SystemConfigUtils.getAzureStorageAccountName(),SystemConfigUtils.getAzureStorageAccountKey()));
            }else{
                logger.info("storageAccount is not null!!!");
            }
            if(blobClient == null){
                blobClient = storageAccount.createCloudBlobClient();
            }else{
                logger.info("blobClient is not null!!!");
            }
            if(container == null){
                container = blobClient.getContainerReference(SystemConfigUtils.getAzureStoragecontainer());
                // Private blob-container with no access for anonymous users
                containerPermissions.setPublicAccess(BlobContainerPublicAccessType.OFF);
                container.uploadPermissions(containerPermissions);
            }else{
                logger.info("container is not null!!!");
            }
            
            CloudBlockBlob blob = container.getBlockBlobReference(fileNameAll);
            blob.delete();
        } catch (StorageException ex) {
            logger.info(String.format("Error returned from the service. Http code: %d and error code: %s", ex.getHttpStatusCode(), ex.getErrorCode()));
            logger.error(ex.getMessage(), ex);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
        finally 
        {
            logger.info("over");
        }
    }

}

 

相关标签: java Azure Storage

上一篇:

下一篇: