How can I consume a service to send a file? I’m using dart with the following code, and I only get 403.
Can someone help me?
final file = File('./README.md');
const accessKey = 'afdda';
const secretKey = 'afda';
final spaceName = 'fada';
final region = 'nyc3';
final fileName = 'README.md';
final date = DateTime.now().toUtc().toIso8601String();
final contentType = ContentType.parse('text/plain');
final acl = 'private';
final contentLength = await file.length();
final stringToSign =
'POST\n$contentType\n$date\nx-amz-acl:$acl\n/$spaceName/$fileName';
final hmacSha1 = Hmac(sha1, utf8.encode(secretKey));
final signature =
base64Url.encode(hmacSha1.convert(utf8.encode(stringToSign)).bytes);
print('$spaceName.$region.digitaloceanspaces.com/$fileName');
final url =
Uri.https('$spaceName.$region.digitaloceanspaces.com', '/$fileName');
final request = await HttpClient().postUrl(url)
..headers.add('Date', date)
..headers.add('Content-Type', contentType.toString())
..headers.add('Content-Length', contentLength.toString())
..headers.add('Authorization', 'AWS $accessKey:$signature')
..headers.add('x-amz-acl', acl)
..add(await file.readAsBytes());
final response = await request.close();
print('Response status code: ${response.statusCode}');
print('Response body:');
await response.transform(utf8.decoder).forEach(print);
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Enter your email to get $200 in credit for your first 60 days with DigitalOcean.
New accounts only. By submitting your email you agree to our Privacy Policy.
Hi there,
As far as I can see the
host
header is missing:For more information about the common headers, you could take a look at the docs here:
https://docs.digitalocean.com/reference/api/spaces-api/#common-headers
On another note, I would personally suggest using the
s3
client instead:Hope that this helps!
Best,
Bobby