微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用新命名空间重写方法 - Azure.Storage.Files.Shares

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

$PAT="$(pat)"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
          
#Get the prevIoUs build commit id
$GetPrevIoUsURL = "https://dev.azure.com/{Org name}/{Project name}/_apis/build/builds?deFinitions={Build deFinition ID}&api-version=6.1-preview.6"
$GetPrevIoUsBuild = Invoke-RestMethod -Uri $GetPrevIoUsURL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get 
$PrevIoUsBuildID=$GetPrevIoUsBuild.value.id[1]
#Write-Host $PrevIoUsBuildID

#Get the changes made to the repository between two given builds
$CompareBuildURL= "https://dev.azure.com/{Org name}/{Project name}/_apis/build/changes?fromBuildId=$($PrevIoUsBuildID)&toBuildId=$($env:BUILD_BUILDNUMBER)&api-version=6.1-preview.2"

#Write-Host $CompareBuildURL

$Comparebuildresult = Invoke-RestMethod -Uri $CompareBuildURL -Headers @{Authorization = "Basic {0}" -f $base64AuthInfo} -Method get 
$Count = $Comparebuildresult.count
#Write-Host $Count

#If the count is 0,fail this build
if ($Count.equals(0)) { 
        throw 'it does not have the latest changes from master (exit 1)'
   }

解决方法

请尝试以下操作:

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; }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?