如何解决Sftp文件上传C#
我在 azure 门户上创建了 Sftp Container Instance
。它有 IP 地址、主机、用户名和密码。
在创建容器实例时,我被要求创建新的存储帐户。所以,我继续了。然后我下载了 FileZilla 客户端并使用 Sftp Container Instance
的凭据与 sftp 连接。
成功连接后,我从 FileZilla 上传了一个文件。
然后,我可以在使用 FileShare
创建的存储帐户的 Sftp Container Instance
中看到上传的文件。
我正在尝试通过 C# 代码将文件上传到 Sftp。
我首先将文件上传到 Azure Storage Account Containers
然后,我想将文件上传到 Sftp FileShare。
我做了以下代码:
if (path != null && fileContents != null)
{
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(path);
cloudBlockBlob.Properties.ContentType = fileMimeType;
await cloudBlockBlob.UploadFromByteArrayAsync(fileContents,fileContents.Length);
using (SftpClient client = new SftpClient("20.74.18.95",22,"sftp","abcd@123"))
{
using (var blobReadStream = await cloudBlockBlob.OpenReadAsync())
{
var remoteFilePath = Path.Combine("/sftpfileshare/");
client.Connect();
client.BufferSize = 4 * 1024;
client.UploadFile(blobReadStream,remoteFilePath,false);
}
}
return cloudBlockBlob.Uri.AbsoluteUri;
}
但它向我抛出异常 Failure
。
我无法弄清楚我做错了什么。
请帮帮我
完整的堆栈跟踪如下:
{"Failure"}
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233088
HelpLink: null
InnerException: null
Message: "Failure"
SerializationRemoteStackTraceString: null
SerializationStackTraceString: " at Renci.SshNet.Sftp.SftpSession.RequestOpen(String path,Flags flags,Boolean nullOnError)\r\n at Renci.SshNet.SftpClient.InternalUploadFile(Stream input,String path,SftpUploadAsyncResult asyncResult,Action`1 uploadCallback)\r\n at Renci.SshNet.SftpClient.UploadFile(Stream input,Boolean canOverride,Action`1 uploadCallback)\r\n at CoreConnect.Interfaces.CrossCutting.BlobStorage.BlobStorageService.UploadFileToBlobAsync(Byte[] fileContents,String fileMimeType,String fileName,ILogger logger) in D:\\Sprint7\\CoreConnect\\CoreConnect\\AzureFunctions\\CoreConnect.AzureFunctions\\CoreConnect.AzureFunctions.CrossCutting\\BlobStorage\\BlobStorageService.cs:line 91"
SerializationWatsonBuckets: null
Source: "Renci.SshNet"
StackTrace: " at Renci.SshNet.Sftp.SftpSession.RequestOpen(String path,Action`1 uploadCallback)\r\n at CoreConnect.Interfaces.CrossCutting.BlobStorage.BlobStorageService.<UploadFileToBlobAsync>d__3.MoveNext() in D:\\Sprint7\\CoreConnect\\CoreConnect\\AzureFunctions\\CoreConnect.AzureFunctions\\CoreConnect.AzureFunctions.CrossCutting\\BlobStorage\\BlobStorageService.cs:line 91"
TargetSite: {Byte[] RequestOpen(System.String,Renci.SshNet.Sftp.Flags,Boolean)}
_HResult: -2146233088
_data: {System.Collections.ListDictionaryInternal}
_dynamicMethods: null
_exceptionMethod: null
_helpURL: null
_innerException: null
_ipForWatsonBuckets: 0x00007ffb2466766f
_message: "Failure"
_remoteStackTraceString: null
_source: null
_stackTrace: {sbyte[192]}
_stackTraceString: null
_watsonBuckets: null
_xcode: -532462766
_xptrs: 0x0000000000000000
解决方法
Blob/Shares 是 Azure 存储帐户下的不同概念。您实际需要的是将文件上传到 Azure 文件共享:
https://docs.microsoft.com/en-us/dotnet/api/overview/azure/storage.files.shares-readme
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。