I have created console application in Program.cs written below code
string fileToBackup = @"C:\Users\\Downloads\rootkey.csv"; // test file
MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(fileToBackup));
string myBucketName = "buckettest455"; //your s3 bucket name goes here
string s3DirectoryName = "justdemodirectory";
string s3FileName = @"rootkey.csv uploaded in 12-9-2014.zip";
AmazonUploader myUploader = new AmazonUploader();
myUploader.sendMyFileToS3(stream, myBucketName, s3DirectoryName, s3FileName);
in AmazonUploader.cs written below code
public bool sendMyFileToS3(System.IO.Stream localFilePath, string bucketName, string subDirectoryInBucket, string fileNameInS3)
{
IAmazonS3 client = new AmazonS3Client(RegionEndpoint.APSouth1);
TransferUtility utility = new TransferUtility(client);
TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();
if (subDirectoryInBucket == "" || subDirectoryInBucket == null)
{
request.BucketName = bucketName; //no subdirectory just bucket name
}
else
{ // subdirectory and bucket name
request.BucketName = bucketName @"/" subDirectoryInBucket;
}
request.Key = fileNameInS3; //file name up in S3
request.InputStream = localFilePath;
utility.Upload(request); //commensing the transfer
return true; //indicate that the file was sent
}
and in App.config i have provided AWSAccessKey and AWSSecretKey
<appSettings>
<add key="AWSProfileName" value="Ashok" />
<add key="AWSAccessKey" value="" />
<add key="AWSSecretKey" value="" />
<!--AWSProfileName is used to reference an account that has been registered with the SDK.
If using AWS Toolkit for Visual Studio then this value is the same value shown in the AWS Explorer.
It is also possible to register an account using the <solution-dir>/packages/AWSSDK-X.X.X.X/tools/account-management.ps1 PowerShell script
that is bundled with the nuget package under the tools folder.
<add key="AWSProfileName" value="" />
-->
</appSettings>
Now whenever i am trying and run application getting below exception
Unable to get IAM security credentials from EC2 Instance Metadata Service.' in line
utility.Upload(request);
CodePudding user response:
You should to create AWS credentials for your env There are 2 ways:
- Create it manually
- Instal aws sdk and use command line Here is manual https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/creds-file.html