Dart
Dart
S3 Upload Binary File from BinData
See more Amazon S3 (new) Examples
Upload a binary file contained in a BinData object to Amazon S3.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final rest = CkRest();
// Connect to the Amazon AWS REST server.
final bTls = true;
final port = 443;
final bAutoReconnect = true;
// Make sure to connect to the region where the bucket is located..
rest.connect('s3-us-west-2.amazonaws.com', port, bTls, bAutoReconnect);
// Provide AWS credentials for the REST call.
final authAws = CkAuthAws();
authAws.accessKey = 'AWS_ACCESS_KEY';
authAws.secretKey = 'AWS_SECRET_KEY';
// Use the correct region..
authAws.region = 'us-west-2';
authAws.serviceName = 's3';
rest.setAuthAws(authAws);
// Set the bucket name via the HOST header.
// In this case, the bucket name is "chilkat.qa".
// (Also make sure to use the correct region.)
rest.host = 'chilkat.qa.s3-us-west-2.amazonaws.com';
// Load a text file into memory.
final pngData = CkBinData();
try {
pngData.loadFile('qa_data/png/anemone.png');
} on ChilkatException {
print('Failed to load file from local filesystem.');
return;
}
// Indicate the Content-Type of our upload. (This is optional)
rest.addHeader('Content-Type', 'image/png');
// Upload the file to Amazon S3.
final sbResponse = CkStringBuilder();
try {
rest.fullRequestBd('PUT', '/images/sea_creatures/anemone.png', pngData, sbResponse);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Did we get a 200 response indicating success?
final statusCode = rest.responseStatusCode;
if (statusCode != 200) {
print('Error response: ${sbResponse.getAsString()}');
print('Status code: $statusCode, Status text: ${rest.responseStatusText}');
return;
}
print('File successfully uploaded.');
}