使用我在另一个主题中找到的代码。尽管accessKeyID和secretAccessKey验证正确,但我在验证时总是会遇到签名不匹配。存储端点对于存储桶也是正确的。 使用Delphi 10 Seattle。 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;
答案 0 :(得分:1)
我稍微修改了你的代码,使用TBytesStream而不是TFileStream来设置TBytes缓冲区的文件内容。你必须阅读流,而不是写它。
现在它不会破坏其内容。
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;
答案 1 :(得分:0)
运行代码我发现了问题。只需更改此行:
select ENOM e, DIR d from Employes e, Departements d where e.ENO=d.DIR;
通过这一行:
Service.UploadObject(Bucket, fileName, bytes, TRUE, nil, nil, amzbaPrivate, info);
远程调用不得出现文件的本地路径(我猜它会混淆服务器,认为您正在尝试访问您无权访问的资源)。