【发布时间】:2021-11-28 15:50:48
【问题描述】:
我对aws s3存储还不是很了解,但是我已经可以使用nuget awssdks做列出/上传/下载对象的基本操作了。
但是,当下载对象时,我必须在我的网络服务器上创建一个文件,然后让我的用户可以使用该文件。
在我看来,我不知道如何执行直接下载到客户端。
这可能吗?如果是,用什么方法?
如何简单地转换我当前的源代码以便在 S3 中直接下载?
非常感谢您的指导。
private void DOWFile(string pFileDir, string pFileName, string pDescription)
{
//Armazena o path do arquivo
string lFilePath = pFileDir + pFileName;
//Verifica se o arquivo existe
if (File.Exists(lFilePath))
{
//Limpa o objeto de resposta
Response.Clear();
//Limpa o nome do arquivo
if (Request.UserAgent.Contains("Firefox")) pDescription = TFileInfo.CONVERTToFileName(pDescription);
//Adiciona o cabeçalho do donwload
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlPathEncode(pDescription) + "\"");
//Seta o tipo do arquivo
Response.ContentType = "application/octet-stream";
//Passa a referência do arquivo
Response.WriteFile(lFilePath);
//Executa o download do arquivo
Response.Flush();
//Finaliza o processamento
HttpContext.Current.Response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
else
{
//Mostra a mensagem de Erro
SETErrorInfo(new TErrorInfo(-1, "DOWFile", TErrorInfo.ErrFileNotFound, "FileNotFound - " + lFilePath, false, true, false, true, false, TErrorInfo.TErrType.etLogic));
}
}
public bool FileDownS3(string pLocalPath, string pFileNameS3)
{
IAmazonS3 lClient_Aws = new AmazonS3Client(Amazon.RegionEndpoint.USEast1);
TransferUtility lUtility = new TransferUtility(lClient_Aws);
string filename = pLocalPath + pFileNameS3;
FileStream fs = File.Create(filename);
fs.Close();
lUtility.Download(filename, gBucketName, pFileNameS3);
return true;
}
【问题讨论】:
-
到目前为止你尝试过什么?我看不到任何 S3 代码?
-
您是在问如何读取文件然后在浏览器中下载文件?
-
@ErmiyaEskandary
codepublic bool FileDownS3(string pLocalPath, string pFileNameS3) { IAmazonS3 lClient_Aws = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); TransferUtility lUtility = new TransferUtility(lClient_Aws);字符串文件名 = pLocalPath + pFileNameS3; FileStream fs = File.Create(filename); fs.Close(); lUtility.Download(filename, gBucketName, pFileNameS3);返回真; }code -
@FabioAlvesFrancelino 请将代码添加到问题中,谢谢 :)
-
@smac2020 是的,如何将文件直接下载到客户端,而无需在我的网络服务器中创建文件。
标签: c# asp.net .net amazon-web-services amazon-s3