· 7 years ago · Jun 01, 2018, 12:56 AM
1public bool Exists(string fileKey, string bucketName)
2{
3 try
4 {
5 response = _s3Client.GetObjectMetadata(new GetObjectMetadataRequest()
6 .WithBucketName(bucketName)
7 .WithKey(key));
8
9 return true;
10 }
11
12 catch (Amazon.S3.AmazonS3Exception ex)
13 {
14 if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
15 return false;
16
17 //status wasn't not found, so throw the exception
18 throw;
19 }
20}
21
22using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKey, secretKey))
23{
24 S3FileInfo s3FileInfo = new Amazon.S3.IO.S3FileInfo(client, "your-bucket-name", "your-file-name");
25 if (s3FileInfo.Exists)
26 {
27 // file exists
28 }
29 else
30 {
31 // file does not exist
32 }
33}
34
35var request = new ListObjectsRequest()
36 .WithBucketName(_bucketName)
37 .WithPrefix(keyPrefix);
38
39 var response = _amazonS3Client.ListObjects(request);
40
41 var exists = response.S3Objects.Count > 0;
42
43 foreach (var obj in response.S3Objects) {
44 // act
45 }
46
47using (IAmazonS3 s3Client = new AmazonS3Client(accessKey, secretKey))
48{
49 S3DirectoryInfo s3DirectoryInfo = new Amazon.S3.IO.S3DirectoryInfo(s3Client, "testbucket");
50 if (s3DirectoryInfo.GetFiles("test*").Any())
51 {
52 //file exists -- do something
53 }
54 else
55 {
56 //file doesn't exist -- do something else
57 }
58}
59
60BasicAWSCredentials credentials = new BasicAWSCredentials("accessKey", "secretKey");
61
62AmazonS3Config configurationAmazon = new AmazonS3Config();
63configurationAmazon.RegionEndpoint = S3Region.EU; // or you can use ServiceUrl
64
65AmazonS3Client s3Client = new AmazonS3Client(credentials, configurationAmazon);
66
67
68S3DirectoryInfo directoryInfo = new S3DirectoryInfo(s3Client, bucketName);
69 bucketExists = directoryInfo.Exists;// true if the bucket exists in other case false.
70
71S3FileInfo info = new S3FileInfo(s3Client, "butcketName", "key");
72bool fileExists = info.Exists; // true if the key Exists in other case false
73
74ListObjectsRequest request = new ListObjectsRequest();
75 try
76 {
77 request.BucketName = "bucketName";
78 request.Prefix = "prefix"; // or part of the key
79 request.MaxKeys = 1; // max limit to find objects
80 ListObjectsResponse response = s3Client .ListObjects(request);
81 return response.S3Objects.Count > 0;
82 }
83
84public boolean exists(AmazonS3 s3, String bucket, String key) {
85 ObjectListing list = s3.listObjects(bucket, key);
86 return list.getObjectSummaries().size() > 0;
87}
88
89s3 = new S3(S3KEY, S3SECRET, false);
90 res = s3->getObjectInfo(bucketName, filename);
91
92NameValueCollection appConfig = ConfigurationManager.AppSettings;
93
94 AmazonS3 s3Client = AWSClientFactory.CreateAmazonS3Client(
95 appConfig["AWSAccessKey"],
96 appConfig["AWSSecretKey"],
97 Amazon.RegionEndpoint.USEast1
98 );
99
100S3DirectoryInfo source = new S3DirectoryInfo(s3Client, "BUCKET_NAME", "Key");
101if(source.Exist)
102{
103 //do ur stuff
104}