首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用新的命名空间- Azure.Storage.Files.Shares重写方法

使用新的命名空间- Azure.Storage.Files.Shares重写方法
EN

Stack Overflow用户
提问于 2021-03-19 13:59:57
回答 1查看 108关注 0票数 0

我想将使用"Microsoft.Azure.Storage.File“的方法重写为"Azure.Storage.Files.Shares”。此方法使用sasuri将文件复制到fileshare。

代码语言:javascript
复制
    /// <summary>
    /// Data model for attachment content
    /// </summary>
    public class Attachment
    {
      /// <summary>
      /// File Name
      /// </summary>
      public string FileName { get; set; }

      /// <summary>
      /// FileContent
      /// </summary>
      public string Content { get; set; }
    
     /// <summary>
     /// File Type
     /// </summary>
     public string ContentType { get; set; }
   }

    /// <summary>
    /// CopyFileToShareAsync
    /// </summary>
    /// <param name="sasUri"></param>
    /// <param name="attachment"></param>
    /// <returns></returns>
    public async Task CopyFileToShareAsync(string sasUri, Attachment attachment)
    {
        var cloudFileDirectory = new CloudFileDirectory(new Uri(sasUri));

        var cloudFile = cloudFileDirectory.GetFileReference(attachment.FileName);

        byte[] bytes = Convert.FromBase64String(attachment.Content);

        await cloudFile.UploadFromStreamAsync(new MemoryStream(bytes));
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-19 14:27:14

请尝试执行以下操作:

代码语言:javascript
复制
class Program
{
    static void Main(string[] args)
    {
        string sasUri = "https://account-name.file.core.windows.net/share-name?sv=2020-04-08&se=2021-03-30T18%3A30%3A00Z&sr=s&sp=cw&sig=ZYXpOsCwP6uBOudbEu3TWwBAPc%2BOefu%2B%2F0lKl5ls3k8%3D";
        Attachment attachment = new Attachment()
        {
            FileName = "sample.text",
            Content = Convert.ToBase64String(Encoding.UTF8.GetBytes("Some content")),
            ContentType = "text/plain"
        };
        CopyFileToShareAsync(sasUri, attachment).GetAwaiter().GetResult();
        Console.WriteLine("Content uploaded");
    }

    static async Task CopyFileToShareAsync(string sasUri, Attachment attachment)
    {
        ShareDirectoryClient shareDirectoryClient = new ShareDirectoryClient(new Uri(sasUri));
        ShareFileClient shareFileClient = shareDirectoryClient.GetFileClient(attachment.FileName);
        byte[] fileContent = Convert.FromBase64String(attachment.Content);
        await shareFileClient.CreateAsync(fileContent.Length, new Azure.Storage.Files.Shares.Models.ShareFileHttpHeaders()
        {
            ContentType = attachment.ContentType
        });
        using (MemoryStream ms = new MemoryStream(fileContent))
        {
            await shareFileClient.UploadAsync(ms);
        }
    }
}

public class Attachment
{
    /// <summary>
    /// File Name
    /// </summary>
    public string FileName { get; set; }

    /// <summary>
    /// FileContent
    /// </summary>
    public string Content { get; set; }

    /// <summary>
    /// File Type
    /// </summary>
    public string ContentType { get; set; }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66703213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档