加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 使用TAmazonStorageService.UploadObject将Content-ty

发布时间:2020-12-15 09:51:37 所属栏目:大数据 来源:网络整理
导读:使用以下Delphi XE2(更新4)代码: var ConInfo: TAmazonConnectionInfo; RespInfo: TCloudResponseInfo; Service: TAmazonStorageService; Content: TBytes; Headers: TStringList;begin ConInfo:=TAmazonConnectionInfo.Create(self); ConInfo.AccountName:
使用以下Delphi XE2(更新4)代码:

var
  ConInfo: TAmazonConnectionInfo;
  RespInfo: TCloudResponseInfo;
  Service: TAmazonStorageService;
  Content: TBytes;
  Headers: TStringList;
begin
  ConInfo:=TAmazonConnectionInfo.Create(self);
  ConInfo.AccountName:='YOUR ACCOUNT NAME';
  ConInfo.AccountKey:='YOUR ACCOUNT KEY';
  ConInfo.Protocol:='http';

  Service:=TAmazonStorageService.Create(ConInfo);
  RespInfo:=TCloudResponseInfo.Create;

  SetLength(Content,128);
  FillMemory(@Content[0],128,Byte('x'));

  Headers:=TStringList.Create;
  Headers.Values['Content-type']:='text/plain';
  if not Service.UploadObject('YOUR BUCKET','test.txt',Content,TRUE,nil,Headers,amzbaPrivate,RespInfo) then
    ShowMessage('Failed:' + RespInfo.StatusMessage);

我总是在调用UploadObject时遇到错误:

Failed:HTTP/1.1 403 Forbidden – The request signature we calculated
does not match the signature you provided. Check your key and signing
method. (SignatureDoesNotMatch)

仅当Content-type设置为’text / plain’,’text / html’或文本内容时才会发生这种情况.使用完全相同的代码,如果您只是将内容类型更改为任何其他内容类型,例如’video / 3gpp’,然后按预期工作,没有错误.上传的对象的实际内容不相关,与获取错误无关.

我已经通过Delphi中的Indy代码进行了跟踪,但是我很难理解为什么文本内容类型总是会出现这个错误.

有任何想法吗?

解决方法

如果将“; charset = ISO-8859-1”附加到Content-Type字符串,则它可以工作:

Headers.Values['Content-type']:='text/plain; charset=ISO-8859-1';

单步执行代码,我看到TIdEntityHeaderInfo.SetHeaders(IdHTTPHeaderInfo.pas)中的Content-Type正在更改,该内容类型是从TIdHTTPProtocol.BuildAndSendRequest(IdHTTP.pas)调用的.

最终,看起来问题是TIdEntityHeaderInfo.SetContentType(IdHTTPHeaderInfo.pas)将字符集附加到内容类型(如果它是’text’且它还没有).它不应该在这些情况下更改内容类型,因为内容类型是要签名的字符串的一部分,因此在签名后更改它会使签名无效.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读