Amazon S3 : List Objects - c#

I have this code to list objects in the folder.
var accessKey = ConfigurationManager.AppSettings["AWSAccessKey"];
var secretKey = ConfigurationManager.AppSettings["AWSSecretKey"];
using (AmazonS3Client s3Client = new AmazonS3Client(accessKey, secretKey, RegionEndpoint.USEast1 ))
{
var prefix = string.Format(#"{0}/", uniqueKey);
ListObjectsRequest request = new ListObjectsRequest();
request.BucketName = bucketName;
request.Delimiter = "/";
request.Prefix = prefix;
do
{
ListObjectsResponse response = s3Client.ListObjects(request);
This throws an exception when I access the folders :
<uri>/bucketname/profile/ // Throws and exception key not found
<uri>/bucketname/profile/profile.png // This is OK
Image was created using the following path having a READ CannedACL.
<uri>/bucketname/profile//profile.png
Error
String key was not found
QUESTION
Why i cannot access the folders (objects)?
Are folders need to set permissions individually for it to be accessible?
UPDATE
I solved the issue
Bucket names should not have a / character
Client given me a bucket name with this format sample/new
But the true bucket name is only sample

Related

The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint

Hi I found this error
The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint
while run following code,
AmazonS3Config S3Config = new AmazonS3Config()
{
ServiceURL = "s3.amazonaws.com",
ForcePathStyle = true,
RegionEndpoint = RegionEndpoint.USEast1,
SignatureVersion = "v4"
};
// Set to a bucket you create
// Create S3 service client.
using (IAmazonS3 s3Client = new AmazonS3Client(kAWSAccessKeyId, kAWSSecretAccessKey, S3Config))
{
// Setup request for putting an object in S3.
PutObjectRequest request = new PutObjectRequest
{
BucketName = kBucketName,
Key = "Contents/SampleData",
ContentBody = "Sample Content"
};
// Make service call and get back the response.
PutObjectResponse response = s3Client.PutObject(request);
}
I have changed all region but in every region it showing same error

Amazon S3 Unable to find credentials in C#

I'm trying to upload an image file to aws s3 storage and get back that image URL. I'm using secret key and access key to create credentials. But when the program runs it it says
"Unable to find credentials" .
Here is my code which i used.
public string sendMyFileToS3(string from,string to, string bucketName, string fileName)
{
BasicAWSCredentials awsCreds = new BasicAWSCredentials(bucketName, fileName);
AmazonS3Client client = new AmazonS3Client(awsCreds);
TransferUtility utility = new TransferUtility(client);
TransferUtilityUploadRequest request = new TransferUtilityUploadRequest();
request.BucketName = bucketName;
request.Key = fileName;
request.FilePath = from;
utility.Upload(request);
string urlString = "";
GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest
{
BucketName = bucketName,
Key = fileName,
Expires = DateTime.Now.AddYears(2)
};
urlString = client.GetPreSignedURL(request1);
Console.WriteLine(urlString);
File.Move(from, to);
return urlString ;
}
In order to create an S3 Client you need to provide your credentials, the region and endpoint:
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonS3Config config = new AmazonS3Config();
config.ServiceURL = "s3.amazonaws.com";
config.RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName("us-east-1");
client = new AmazonS3Client(credentials, config);
The possible regions are listed here, and depend on where you created your bucket being us-east-1 the default value.
While the possible endpoints are this three:
s3.amazonaws.com
s3-accelerate.amazonaws.com
s3-accelerate.dualstack.amazonaws.com
The first one being the standard one since the others need you to configure your bucket like it's explained here.
I am going to take a guess and say you have a conflict between the credentials your app is using and other credentials you may have installed onto your dev or test machine, i.e. in the credentials file or your app.config.
I would check and make sure you are only using a single method to provide those credentials to the program.
THis link shows the priority the SDK will look for the credentials:
http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html

Creating folder using Google Drive API gives invalid_grant but can create actual file with same connection

I can setup a Google user content connection with Google API and can get folders and even upload/create files to folders. But the same connection used to create a new regular folder anywhere gives the error invalid_grant with no further information. I am passing in a valid folderId to set as parent. I have tried different scopes and a long shot of syncing my clock like I have seen on other solutions. I can't seem to figure out the issue. Anyone else have seen this?
Here is code that works to upload a file to a folder:
public async Task<string> Upload(string localFilePath, string folderId, string fileName, string refreshToken, string userId)
{
var file = new File();
file.MimeType = GetMimeType(localFilePath);
file.Name = fileName;
file.Parents = new List<string> { folderId };
var driveService = await GetDriveService(refreshToken, userId);
// File's content.
var byteArray = System.IO.File.ReadAllBytes(localFilePath);
var stream = new System.IO.MemoryStream(byteArray);
var request = driveService.Files.Create(file, stream, file.MimeType);
await request.UploadAsync();
return request.ResponseBody.Id;
}
Here is what I've been trying in creating a folder:
public async Task<string> CreateFolder(string folderId, string folderName, string refreshToken, string userId)
{
var newFolderId = string.Empty;
var driveService = await GetDriveService(refreshToken, userId);
FilesResource.ListRequest list = driveService.Files.List();
var file = new File();
file.MimeType = "application/vnd.google-apps.folder";
file.Name = folderName;
file.Parents = new List<string> { folderId };
var request = driveService.Files.Create(file);
request.Fields = "Id";
var newFolder = await request.ExecuteAsync();
return newFolder.Id;
}
And, here is the method in defining driveService:
private async Task<DriveService> GetDriveService(string refreshToken, string userId)
{
var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
clientSecrets: new ClientSecrets { ClientId = "xyz.apps.googleusercontent.com", ClientSecret = "xyz" },
scopes: new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile },
dataStore: new MemoryDataStore(userId, refreshToken),
user: userId,
taskCancellationToken: CancellationToken.None);
var driveService = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "XYZ"
});
return driveService;
}
As mentioned in creating a folder,
In the Drive API, a folder is essentially a file — one identified by the special folder MIME type application/vnd.google-apps.folder. You can create a new folder by inserting a file with this MIME type and a folder title. Do not include an extension when setting a folder title.
Please try using the given code:
File fileMetadata = new File();
fileMetadata.setName("Invoices");
fileMetadata.setMimeType("application/vnd.google-apps.folder");
File file = driveService.files().create(fileMetadata)
.setFields("id")
.execute();
System.out.println("Folder ID: " + file.getId());
I apologize, I am a victim of a copy/paste or fat finger error that I just did not notice unfortunately until now. I am managing multiple cloud connections in this app for similar purposes, and at the beginning of the call in this case to create folder I either copy/pasted or fat fingered an enum value that was improperly requesting to get the saved refreshtoken from a different cloud provider ala one drive or dropbox. Once switched to pull the saved google refreshtoken, it works. Well, at least there is a working example of create folder and upload file above :-) Btw, the line request.Fields = "Id"; needs to be request.Fields = "id"; where id needs to be lower case...otherwise a different error pops up saying the field is invalid. Thanks.

AWS upload - bucket not found

I have this in app.config:
<appSettings>
<add key="AWSProfileName" value="myName"/>
<add key="AWSRegion" value="eu-west-1" />
</appSettings>
Then I try to run this:
using Amazon.S3;
using Amazon.S3.Model;
namespace createCSV
{
class S3
{
public void uploadObject()
{
//IAmazonS3 client;
string AwsAccessKey = "xxxxxxxxxxxxx";
string AwsSecretKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
IAmazonS3 client = new AmazonS3Client(AwsAccessKey,AwsSecretKey);
PutObjectRequest request = new PutObjectRequest()
{
BucketName = #"http://s3-eu-west-1.amazonaws.com/bucketName/",
Key = #"test/blah.txt",
FilePath = #"P:\data_analysis\foo\blah.txt"
};
PutObjectResponse response2 = client.PutObject(request); //<<exception here
}
}
}
I get an exception on the line marked saying:
Output>>
Cannot find or open the PDB file. Exception thrown:
'Amazon.S3.AmazonS3Exception' in AWSSDK.dll
AmazonS3exception was unhandled:
An unhandled exception of type 'Amazon.S3.AmazonS3Exception' occurred
in AWSSDK.dll
Additional information: The specified bucket does not exist
I've tried lots of different configurations for the line BucketName = #"http://s3-eu-west-1.amazonaws.com/bucketName/" with still the same exception - can anyone help?
I think,You forgot insert BucketName property in the PutObjectRequest.
see the example below to upload a file to AmazonS3.
string secretKey = "your secret key";
string accessKey = "your access key";
AmazonS3Client client = new AmazonS3Client(secretKey, accessKey, RegionEndpoint.EUWest1);// choose your region endpoin for this example I am usgin eu-west-1
PutObjectRequest uploadObjectRe = new PutObjectRequest()
{
BucketName = "qabucketireland",//your bucket name not full URL
Key = "test/blah.txt",
FilePath = #"c:\documents\script.txt"
};
client.PutObject(uploadObjectRe);
Another way to upload a file Could be using TransferUtility class see the example below:
string secretKey = "your secret key";
string accessKey = "your access Key";
AmazonS3Client client = new AmazonS3Client(secretKey, accessKey, RegionEndpoint.EUWest1);
TransferUtility transfer = new TransferUtility(client);
transfer.Upload(#"c:\documents\script - Copy.txt", "qabucketireland", #"test/blah2.txt");
Also You could read this "How to upload a file to amazon S3 super easy using c#"
The BucketName parameter to PutObjectRequest should be the name of the bucket (e.g. bucketName) rather than the fully qualified endpoint (e.g. http://s3-eu-west-1.amazonaws.com/bucketName/). You can find more info about the SDK here.

Amazon MWS client library C# AWS Access Key Id error

I am using Amazon C# client library to get product information and keep getting an error " The AWS Access Key Id you provided does not exist in our records." (Yes, I tried the seller forum, but didn't get an answer there). When I use the same Access Key using their scratchpad, I get the correct response. I did see this post (Getting 'The AWS Access Key Id you provided does not exist in our records' error with Amazon MWS) and tried swapping the parameters and that didn't work. Here is my C# code. Any help would be greatly appreciated
string AccessKey = "xxx";
string SecretKey = "xxx";
string AppName = "ProductFunctionsApp";
string AppVersion = "1.0";
string ServiceURL = "https://mws.amazonservices.com/Products/2011-10-01";
string SellerId="xxxx";
string MarketPlaceId = "xxx";//US
//right now MWSAuthToken is only if a developer is using a sellers account
MarketplaceWebServiceProductsConfig config = new MarketplaceWebServiceProductsConfig();
config.ServiceURL = ServiceURL;
config.SignatureMethod = "HmacSHA256";
config.SignatureVersion = "2";
MarketplaceWebServiceProductsClient client = new MarketplaceWebServiceProductsClient(AppName, AccessKey, SecretKey, AppVersion, config);
ASINListType type = new ASINListType();
List<string> ASINList = new List<string>();
ASINList.Add("B001E6C08E");
type.ASIN = ASINList;
;
GetCompetitivePricingForASINRequest request = new GetCompetitivePricingForASINRequest();
request.SellerId = SellerId;
request.ASINList = type;
request.MarketplaceId = MarketPlaceId;
GetCompetitivePricingForASINResponse response = client.GetCompetitivePricingForASIN(request);
Some of their API Clients have the class initialization parameters defined in different orders.
So if you copy and paste the initialization code you'll end up with the application name being sent instead of the access key.
var service = new MarketplaceWebServiceProductsClient(
applicationName, applicationVersion, accessKeyId, secretAccessKey, config);
And it's different here:
var service = new FBAInventoryServiceMWSClient(
accessKeyId, secretAccessKey, applicationName, applicationVersion, config);
Just check each one carefully.

Categories