从 azure 函数应用程序上传文件到 azure 文件共享
Uploading file to azure file share from azure function app
我有一个函数应用程序和 Azure 文件共享。我想使用 url 从 Internet 上传文件到我的 Azure 文件共享存储。
private ShareFileClient GetShareFile(string filePath)
{
string connectionString = "My connection string";
return new ShareFileClient(connectionString, "temp", filePath);
}
ShareFileClient destFile = GetShareFile(filePath);
// Start the copy operation
await destFile.StartCopyAsync(new Uri(DownloadUrl));
但是这段代码没有按预期工作。它抛出错误“未经授权
RequestId:000db1ff-801a-000a-0602-b24449000000
Time:2020-11-03T16:58:36.5697281Z
状态:401(未授权)
ErrorCode: CannotVerifyCopySource" 。任何帮助将不胜感激
编写和读取的简单代码:
string con_str = "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net";
string sharename = "test";
string filename = "test.txt";
string directoryname = "testdirectory";
ShareServiceClient shareserviceclient = new ShareServiceClient(con_str);
ShareClient shareclient = shareserviceclient.GetShareClient(sharename);
ShareDirectoryClient sharedirectoryclient = shareclient.GetDirectoryClient(directoryname);
//write data.
ShareFileClient sharefileclient_in = sharedirectoryclient.CreateFile(filename,1000);
string filecontent_in = "This is the content of the file.";
byte[] byteArray = Encoding.UTF8.GetBytes(filecontent_in);
MemoryStream stream1 = new MemoryStream(byteArray);
stream1.Position = 0;
sharefileclient_in.Upload(stream1);
//read data.
ShareFileClient sharefileclient_out = sharedirectoryclient.GetFileClient(filename);
Stream stream2 = sharefileclient_out.Download().Value.Content;
StreamReader reader = new StreamReader(stream2);
string filecontent_out = reader.ReadToEnd();
以上代码在我这边工作正常,你只需要先将文件转换为流。
我有一个函数应用程序和 Azure 文件共享。我想使用 url 从 Internet 上传文件到我的 Azure 文件共享存储。
private ShareFileClient GetShareFile(string filePath)
{
string connectionString = "My connection string";
return new ShareFileClient(connectionString, "temp", filePath);
}
ShareFileClient destFile = GetShareFile(filePath);
// Start the copy operation
await destFile.StartCopyAsync(new Uri(DownloadUrl));
但是这段代码没有按预期工作。它抛出错误“未经授权 RequestId:000db1ff-801a-000a-0602-b24449000000 Time:2020-11-03T16:58:36.5697281Z 状态:401(未授权) ErrorCode: CannotVerifyCopySource" 。任何帮助将不胜感激
编写和读取的简单代码:
string con_str = "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net";
string sharename = "test";
string filename = "test.txt";
string directoryname = "testdirectory";
ShareServiceClient shareserviceclient = new ShareServiceClient(con_str);
ShareClient shareclient = shareserviceclient.GetShareClient(sharename);
ShareDirectoryClient sharedirectoryclient = shareclient.GetDirectoryClient(directoryname);
//write data.
ShareFileClient sharefileclient_in = sharedirectoryclient.CreateFile(filename,1000);
string filecontent_in = "This is the content of the file.";
byte[] byteArray = Encoding.UTF8.GetBytes(filecontent_in);
MemoryStream stream1 = new MemoryStream(byteArray);
stream1.Position = 0;
sharefileclient_in.Upload(stream1);
//read data.
ShareFileClient sharefileclient_out = sharedirectoryclient.GetFileClient(filename);
Stream stream2 = sharefileclient_out.Download().Value.Content;
StreamReader reader = new StreamReader(stream2);
string filecontent_out = reader.ReadToEnd();
以上代码在我这边工作正常,你只需要先将文件转换为流。