zoukankan      html  css  js  c++  java
  • Azure文件上传下载删除(D365可以直接用)

    1.将文件上传到Azure的特定容器下;

    下面以将文件上传到Azure的Container下。

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Auth;
    using Microsoft.WindowsAzure.Storage.Blob;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    namespace AzureCall
    {
        class Program
        {
            static void Main(string[] args)
            {
                var fileName = Guid.NewGuid() + ".CSV";
                byte[] byteArray = new byte[10000];
                //Make sure a file was posted
                string contentType = "application/octet-stream";
        
                Stream stream = new MemoryStream(byteArray);
               //这里文件流如果不知道如何获取,可以看下我的关于上传下载工具的文档。
                string accountName = "存储账户";
                string accessKey = "密码";
                string containerName = "容器名称";
                // BLOB credential
                var credential = new StorageCredentials(accountName, accessKey);
                var storageAccount = new CloudStorageAccount(credential, true);
                // BLOB client
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer container = blobClient.GetContainerReference(containerName);
                container.CreateIfNotExistsAsync();
                // Get csv file reference
                CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(fileName);
                // Create or overwrite the blob with contents from a local file
                cloudBlockBlob.Properties.ContentType = contentType;
                cloudBlockBlob.UploadFromStream(stream);
            }
        }
    }
    2.删除Azure上的文件;
     
    string accountName = "存储账户";
    string accessKey = "密码";
    string containerName = "容器名称";
    // BLOB credential
    var credential = new StorageCredentials(accountName, accessKey);
    var storageAccount = new CloudStorageAccount(credential, true);
    // BLOB client
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);
    // Retrieve reference to a blob named
    CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
    if (blob.Exists())
    blob.DeleteAsync();
     
    3.从Azure的特定容器下载文件;
     
    string accountName = "存储账户";
    string accessKey = "密码";
    string containerName = "容器名称";
    // BLOB credential
    var credential = new StorageCredentials(accountName, accessKey);
    var storageAccount = new CloudStorageAccount(credential, true);
    // BLOB client
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);
    // Get csv file reference
    CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(fileName);
    string filePath = @"C:Alfred";
    if (!Directory.Exists(filePath))
    {
           Directory.CreateDirectory(filePath);
    }
    cloudBlockBlob.DownloadToFile(filePath, FileMode.OpenOrCreate);
  • 相关阅读:
    bzoj 4883 [Lydsy1705月赛]棋盘上的守卫——并查集(思路!)
    洛谷 1979 华容道——最短路+dp
    51nod 1443 路径和树——最短路生成树
    hdu 2222 Keywords Search——AC自动机
    bzoj 2067 [Poi2004]SZN——二分+贪心
    洛谷 1084 疫情控制——二分答案+贪心(贪心思路!)
    CF 1042A Benches——二分答案(水题)
    洛谷 1314 聪明的质监员——二分答案
    洛谷P3690 LCT模板
    bzoj1875 [SDOI2009]HH去散步——矩阵快速幂
  • 原文地址:https://www.cnblogs.com/alfred-cn/p/13033522.html
Copyright © 2011-2022 走看看