Reputation: 660
I have my files stored at Amazon S3 cloud and want to get them using C# .NET. I have no issues in getting the file if I use complete file name as the key for retrieving the file.
keyName = "myfile.jpg";
client.GetObject(new GetObjectRequest { BucketName = bucketName, Key = keyName });
Now, what if I don't know the file extension of image, it might be "myfile.jpg", "myfile.png" or "myfile.gif". I have "myfile" as the only information with me.
One thing is sure that the file would be the image only.
Is there any way to get the object by only file name, ("myfile")? Or in Amazon S3, we can change the key of the file as "myFile" or whatsoever we want to be?
Any help would be appreciated.
Thanks!
Upvotes: 0
Views: 8902
Reputation: 16540
You have an API to get the list of names of the Files (S3 keys) and you have another API to fetch the file (S3 key).
This code gives you the list of all the keys (objects) from your S3 bucket.
// Create a client
AmazonS3Client client = new AmazonS3Client();
// List all objects
ListObjectsRequest listRequest = new ListObjectsRequest
{
BucketName = "SampleBucket",
};
ListObjectsResponse listResponse;
do
{
// Get a list of objects
listResponse = client.ListObjects(listRequest);
foreach (S3Object obj in listResponse.S3Objects)
{
Console.WriteLine("Object - " + obj.Key);
Console.WriteLine(" Size - " + obj.Size);
Console.WriteLine(" LastModified - " + obj.LastModified);
Console.WriteLine(" Storage class - " + obj.StorageClass);
}
// Set the marker property
listRequest.Marker = listResponse.NextMarker;
} while (listResponse.IsTruncated);
Taken from http://docs.aws.amazon.com/sdkfornet/latest/apidocs/Index.html
Upvotes: 3