[英]Upload files to Amazon S3 with Delphi
using code I found in another topic.使用我在另一个主题中找到的代码。 I'm always getting a signature mismatch when authenticating, although accessKeyID and secretAccessKey are verified correct.
我在验证时总是遇到签名不匹配,尽管 accessKeyID 和 secretAccessKey 被验证正确。 Also the storage endpoint is correct for the bucket.
此外,存储端点对于存储桶也是正确的。 Using Delphi 10 Seattle.
使用 Delphi 10 西雅图。 Possible that the CloudComponents have a problem with the region or (local) time ?
可能是 CloudComponents 的区域或(本地)时间有问题?
Function Amazon_Upload (fileName, bucket, accessKeyID, secretAccessKey : String) : TCallResult;
var
Service: TAmazonStorageService;
ConAmazon: TAmazonConnectionInfo;
info : TCloudResponseInfo;
upload_stream : TFileStream;
bytes : TBytes;
begin
// create file
upload_stream := TFileStream.Create(fileName,fmOpenRead);
try
// filestream to tBytes
upload_stream.Position := 0;
SetLength(bytes, upload_stream.Size);
upload_stream.Write(bytes[0], upload_stream.Size);
ConAmazon := TAmazonConnectionInfo.Create(nil);
try
// amazon connection parameters
ConAmazon.AccountKey := secretAccessKey;
ConAmazon.AccountName := accessKeyID;
ConAmazon.QueueEndpoint := 'queue.amazonaws.com';
ConAmazon.StorageEndpoint := 's3-eu-central-1.amazonaws.com';
ConAmazon.TableEndpoint := 'sdb.amazonaws.com';
ConAmazon.UseDefaultEndpoints := False;
// storage objects
info := TCloudResponseInfo.Create;
Service := TAmazonStorageService.Create(ConAmazon);
try
// upload document to storage
Service.UploadObject(Bucket, fileName, bytes, TRUE, nil, nil, amzbaPrivate, info);
// get results
Result.Callstatus := info.StatusCode;
Result.Success := info.StatusCode in [Ord(rrOK),ord(rrCreated),ord(rrNoContent)];
Result.Response := TJSONObject.ParseJSONValue(info.StatusMessage);
If Assigned(OnLog) Then
FOnlog(info.StatusCode, ConAmazon.StorageEndpoint + #13#10 + bucket + #13#10 + accessKeyID + #13#10 + secretAccessKey, info.StatusMessage, '');
finally
info.Free;
Service.Free;
end;
finally
ConAmazon.Free;
end;
finally
upload_stream.Free;
end;
end;
I have modified slightly your code to use a TBytesStream instead of a TFileStream to set the file content on a TBytes buffer.我稍微修改了您的代码,以使用 TBytesStream 而不是 TFileStream 来设置 TBytes 缓冲区上的文件内容。 But mostly you have to read the stream, not write it.
但大多数情况下,您必须读取流,而不是写入流。
Now it doesn't corrupt its content.现在它不会破坏其内容。
Function Amazon_Upload (fileName, bucket, accessKeyID, secretAccessKey: String) : TCallResult;
var
Service: TAmazonStorageService;
ConAmazon: TAmazonConnectionInfo;
info : TCloudResponseInfo;
upload_stream : TBytesStream;
bytes : TBytes;
begin
// create file
upload_stream := TBytesStream.Create;
upload_stream.LoadFromFile(filename);
try
// filestream to tBytes
upload_stream.Position := 0;
SetLength(bytes, upload_stream.Size);
upload_stream.ReadBuffer(bytes, upload_stream.Size);
ConAmazon := TAmazonConnectionInfo.Create(nil);
try
// amazon connection parameters
ConAmazon.AccountKey := secretAccessKey;
ConAmazon.AccountName := accessKeyID;
ConAmazon.QueueEndpoint := 'queue.amazonaws.com';
ConAmazon.StorageEndpoint := 's3-eu-central-1.amazonaws.com';
ConAmazon.TableEndpoint := 'sdb.amazonaws.com';
ConAmazon.UseDefaultEndpoints := False;
// storage objects
info := TCloudResponseInfo.Create;
Service := TAmazonStorageService.Create(ConAmazon);
try
// upload document to storage
Service.UploadObject(Bucket, ExtractFileName(fileName), bytes, TRUE, nil, nil, amzbaPrivate, info);
// get results
Result.Callstatus := info.StatusCode;
Result.Success := info.StatusCode in [Ord(rrOK),ord(rrCreated),ord(rrNoContent)];
Result.Response := TJSONObject.ParseJSONValue(info.StatusMessage);
If Assigned(OnLog) Then
FOnlog(info.StatusCode, ConAmazon.StorageEndpoint + #13#10 + bucket + #13#10 + accessKeyID + #13#10 + secretAccessKey, info.StatusMessage, '');
finally
info.Free;
Service.Free;
end;
finally
ConAmazon.Free;
end;
finally
upload_stream.Free;
end;
end;
Running your code I have found the problem.运行你的代码我发现了问题。 Just change this line :
只需更改此行:
Service.UploadObject(Bucket, fileName, bytes, TRUE, nil, nil, amzbaPrivate, info);
By this line :通过这一行:
Service.UploadObject(Bucket, ExtractFileName(fileName), bytes, TRUE, nil, nil, amzbaPrivate, info);
The local path of your file must not be present on the remote call (I guess it confuses the server, thinking that you are trying to reach a resource you have not permissions to).您的文件的本地路径不能出现在远程调用中(我猜它会混淆服务器,认为您正在尝试访问您没有权限的资源)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.