简体   繁体   English

使用Delphi从Amazon下载文件

[英]Downloading a file from Amazon with Delphi

I am working with an API that provides me with the URL of a file and I wish to download the file to file or stream using Delphi XE. 我正在使用API​​为我提供文件的URL,我希望使用Delphi XE将文件下载到文件或流。

The URL is in this form:- 网址采用以下形式: -

https://xxxxxxx.s3-us-west-2.amazonaws.com/xxx/Reports/414_20160114021919.pdf?AWSAccessKeyId=xxxxxxxxxxxxxx&Expires=1478915858&response-content-disposition=attachment%3Bfilename%3D%22123%20Test%20St%20Dallas__2016_01_14_03_16_00.pdf%22&Signature=xxxxxxxxxxx%3D

where I have xxxxxxxxx'ed out the sensitive security stuff. 我有xxxxxxxxx的敏感安全的东西。

If I paste the URL into Chrome, it finds the file and brings up the save dialog. 如果我将网址粘贴到Chrome中,它会找到该文件并显示保存对话框。

In my Delphi Program I'm using URLMon with the following code (happily burgled and adapted from answers to similar questions here on SO) :- 在我的Delphi程序中,我正在使用带有以下代码的URLMon(很高兴地窃听并改编自SO的类似问题的答案): -

function TdmXXXXXXXXX.DownloadFile(AURL, AExtWithDot: string): string;
// returns temp file name or empty string if failed;
var
  sTempFileName: string;
  iError: integer;
begin
  sTempFileName := apmDM.GetTempFileName(AExtWithDot, True);
  iError := UrlDownloadToFile(nil, PChar(AURL), PChar(sTempFileName), 0, nil);
  if iError = S_OK then
    Result := sTempFileName
  else
    Result := '';
  showmessage(SysUtils.IntToHex(iError,8));
end;

The showmessage returns "800C000D" showmessage返回“800C000D”

In URLMon:- 在URLMon: -

$EXTERNALSYM INET_E_UNKNOWN_PROTOCOL}
INET_E_UNKNOWN_PROTOCOL            = HResult($800C000D);

So I'm assuming that the problem relates to stuff after the "?" 所以我假设问题与“?”之后的东西有关。 in the URL, "AWSAccessKeyId=" etc. 在URL中,“AWSAccessKeyId =”等。

Not sure where to go from here - searched here and with Mr Google ... 不知道从哪里开始 - 在这里和谷歌先生一起搜索......

Cheers Jeff 干杯杰夫

Though you'll typically find pointers to the Indy TIdHTTP component, my personal favorite is including the Microsoft XML library (MSXML2_TLB), and using the XmlHTTPRequest component. 虽然您通常会找到指向Indy TIdHTTP组件的指针,但我个人最喜欢的是包括Microsoft XML库(MSXML2_TLB)和使用XmlHTTPRequest组件。 If you use a TStreamAdapter on it's responseStream property, you'll have a stream to a resource over an URL. 如果在它的responseStream属性上使用TStreamAdapter ,那么您将通过URL获得资源流。

I scratched all that I had done and restarted from scratch. 我抓了所有的东西,从头开始重新开始。

It all worked using URLMon and URLDownloadToFile(nil, PChar(sReportURL), PChar(sTempFileName), 0, nil); 这一切都使用URLMon和URLDownloadToFile(nil, PChar(sReportURL), PChar(sTempFileName), 0, nil);

I have pored over the code and can't see any difference between reported problem code and my final code. 我仔细研究了代码,看不出报告的问题代码和最终代码之间的区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM