我刚刚开始使用 Blazor,我想尝试制作一个商店/作品集类型的网站,其中一个页面是画廊(类似于 Instagram 的个人资料)。我打算使用 AWS S3 来存储照片,但我似乎无法正确加载照片。
我还使用 SSH.NET 连接到 S3 服务器。
HTML 部分
<body>
<div id="feed" class="centered">
@for (int l = 0; l < @GetLineCount(); l++) //each line has a max of 5 images
{
<div class="feed-line">
@for (int c = 0; c < 5; c++)
{
<img src="@imagesPaths[@l * 5 + @c]" />
}
</div>
}
</div>
</body>
对于 C# 部分
@code {
public List<string> imagesPaths = new();
protected override void OnInitialized()
{
var privateKey = new PrivateKeyFile(@"PATH TO KEY");
using SftpClient client = new(serverUri, 22, username, privateKey);
client.Connect();
if (client.IsConnected)
{
var list = client.ListDirectory("bucket-name/gallery/"); //Correctly retrieves a list with all the files to be loaded
foreach (var sftpFile in list)
{
imagesPaths.Add(sftpFile.FullName); //This would then be retrieved on the html portion
}
}
client.Disconnect();
}
}
我当前的问题是知道如何将 SFTP 图像 URI 转换为 HTTP 图像 URI。
wwwroot
或 我对 Blazor 一无所知。
但总的来说:
在您的
OnInitialized
代码中将文件下载到网络服务器。并使 <img src>
URL 指向那些下载的文件。
<img src="/some/temporary/path/on/server/image.jpg">
或者让您的
<img src>
URL 指向您的 Web 服务器的脚本,该脚本会将单个文件从 SFTP 服务器动态传输到 Web 浏览器。
<img src="some/script/on/server?download=image.jpg">
另一个选项是预签名 S3 URL。