问题描述
我想将使用“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; }
}