diff --git a/S3Demo/GetObject.cs b/S3Demo/GetObject.cs
deleted file mode 100644
index c6e7999..0000000
--- a/S3Demo/GetObject.cs
+++ /dev/null
@@ -1,85 +0,0 @@
-// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
-// SPDX-License-Identifier: Apache-2.0
-
-namespace S3Demo
-{
- using System;
- using System.IO;
- using System.Threading.Tasks;
- using Amazon;
- using Amazon.Runtime;
- using Amazon.S3;
- using Amazon.S3.Model;
-
- ///
- /// This example shows how to use the Amazon Simple Storage System
- /// (Amazon S3) client to copy an object from an Amazon S3 bucket to
- /// another location such as your local system. The code uses the AWS
- /// SDK for .NET version 3.7 and .NET Core 5.0.
- ///
- class GetObject
- {
- public static async Task Readobjectdata(string bucketName, string keyName, BasicAWSCredentials credentials, AmazonS3Config conf)
- {
- //const string bucketName = "doc-example-bucket";
- //const string keyName = "filetodownload";
-
- // If the Amazon Region where the S3 bucket was created is not
- // the same as the region defined for the default user, specify
- // the region as a parameter to the client constructor.
- // For example: RegionEndpoint.USWest2;
- IAmazonS3 client = new AmazonS3Client(credentials, conf);
- await ReadObjectDataAsync(client, bucketName, keyName);
- }
-
- ///
- /// This method copies the contents of the object keyName to another
- /// location, for example, to your local system.
- ///
- /// The initialize S3 client used to call
- /// GetObjectAsync.
- /// The name of the S3 bucket which contains
- /// the object to copy.
- /// The name of the object you want to copy.
- static async Task ReadObjectDataAsync(IAmazonS3 client, string bucketName, string keyName)
- {
- string responseBody = string.Empty;
-
- try
- {
- GetObjectRequest request = new GetObjectRequest
- {
- BucketName = bucketName,
- Key = keyName,
- };
-
- using (GetObjectResponse response = await client.GetObjectAsync(request))
- using (Stream responseStream = response.ResponseStream)
- using (StreamReader reader = new StreamReader(responseStream))
- {
- // Assume you have "title" as medata added to the object.
- string title = response.Metadata["x-amz-meta-title"];
- string contentType = response.Headers["Content-Type"];
- int tag = response.TagCount;
- foreach (var item in response.Key)
- {
- Console.WriteLine($"Object tag: {item}");
- }
- Console.WriteLine($"Object metadata, Title: {title}");
- Console.WriteLine($"Content type: {contentType}");
-
- // Retrieve the contents of the file.
- responseBody = reader.ReadToEnd();
-
- // Write the contents of the file to disk.
- string filePath = $"C:\\Temp\\copy_of_{keyName}";
- }
- }
- catch (AmazonS3Exception e)
- {
- // If the bucket or the object do not exist
- Console.WriteLine($"Error: '{e.Message}'");
- }
- }
- }
-}
\ No newline at end of file
diff --git a/S3Demo/Model/BucketModel.cs b/S3Demo/Model/BucketModel.cs
new file mode 100644
index 0000000..223154b
--- /dev/null
+++ b/S3Demo/Model/BucketModel.cs
@@ -0,0 +1,46 @@
+// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
+// SPDX - License - Identifier: Apache - 2.0
+
+using Amazon;
+using Amazon.Runtime;
+using Amazon.S3;
+using Amazon.S3.Model;
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+
+namespace S3Demo.Model
+{
+ // This example uses the AWS SDK for .NET to check isExist the Amazon Simple Storage
+ // Service (Amazon S3) buckets belonging to the default account. This code
+ // was written using AWS SDK for .NET v3.5 and .NET Core 5.0.
+ class BucketModel
+ {
+ private static IAmazonS3 _s3Client;
+
+ ///
+ /// 判断存储桶是否存在
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static async Task isExistBucket(string bucketName, BasicAWSCredentials credentials, AmazonS3Config conf)
+ {
+ bool flag = false;
+ _s3Client = new AmazonS3Client(credentials, conf);
+ var response = await _s3Client.ListBucketsAsync();
+ List bucketList = response.Buckets;
+ bucketList.ForEach(b => Console.WriteLine($"Bucket name: {b.BucketName}, created on: {b.CreationDate}"));
+ foreach (var item in bucketList)
+ {
+ if (item.BucketName == bucketName)
+ {
+ flag = true;
+ }
+ }
+ return flag;
+ }
+
+ }
+}
diff --git a/S3Demo/ObjectTag.cs b/S3Demo/Model/ObjectModel.cs
similarity index 53%
rename from S3Demo/ObjectTag.cs
rename to S3Demo/Model/ObjectModel.cs
index 43cdfa7..3043a04 100644
--- a/S3Demo/ObjectTag.cs
+++ b/S3Demo/Model/ObjectModel.cs
@@ -1,23 +1,20 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
-// SPDX-License-Identifier: Apache-2.0
+// SPDX - License - Identifier: Apache - 2.0
-namespace S3Demo
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Threading.Tasks;
+using Amazon;
+using Amazon.Runtime;
+using Amazon.S3;
+using Amazon.S3.Model;
+
+namespace S3Demo.Model
{
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using Amazon;
- using Amazon.Runtime;
- using Amazon.S3;
- using Amazon.S3.Model;
-
- ///
- /// This example shows how to work with tags in Amazon Simple Storage
- /// System (Amazon S3) objects. The example was created using the AWS SDK
- /// for .NET version 3.7 and .NET Core 5.0.
- ///
- public class ObjectTag
+ class ObjectModel
{
+ #region 发送对象(带标签)
public static async Task Putobjectswithtags(string bucketName, string keyName, string filePath, string[] tags, BasicAWSCredentials credentials, AmazonS3Config conf)
{
//string bucketName = "doc-example-bucket";
@@ -112,5 +109,77 @@ namespace S3Demo
$"Error: '{ex.Message}'");
}
}
+ #endregion
+
+ #region 查找对象
+ public static async Task Readobjectdata(string bucketName, string keyName, BasicAWSCredentials credentials, AmazonS3Config conf)
+ {
+ //const string bucketName = "doc-example-bucket";
+ //const string keyName = "filetodownload";
+
+ // If the Amazon Region where the S3 bucket was created is not
+ // the same as the region defined for the default user, specify
+ // the region as a parameter to the client constructor.
+ // For example: RegionEndpoint.USWest2;
+ IAmazonS3 client = new AmazonS3Client(credentials, conf);
+ await ReadObjectDataAsync(client, bucketName, keyName);
+ }
+
+ ///
+ /// This method copies the contents of the object keyName to another
+ /// location, for example, to your local system.
+ ///
+ /// The initialize S3 client used to call
+ /// GetObjectAsync.
+ /// The name of the S3 bucket which contains
+ /// the object to copy.
+ /// The name of the object you want to copy.
+ static async Task ReadObjectDataAsync(IAmazonS3 client, string bucketName, string keyName)
+ {
+ string responseBody = string.Empty;
+
+ try
+ {
+ GetObjectRequest request = new GetObjectRequest
+ {
+ BucketName = bucketName,
+ Key = keyName,
+ };
+ // Get tag
+ GetObjectTaggingRequest getTagsRequest = new()
+ {
+ BucketName = bucketName,
+ Key = keyName,
+ };
+ GetObjectTaggingResponse objectTags = await client.GetObjectTaggingAsync(getTagsRequest);
+ // Display the tag values.
+ objectTags.Tagging
+ .ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}"));
+
+ using (GetObjectResponse response = await client.GetObjectAsync(request))
+ using (Stream responseStream = response.ResponseStream)
+ using (StreamReader reader = new StreamReader(responseStream))
+ {
+ // Assume you have "title" as medata added to the object.
+ string title = response.Metadata["x-amz-meta-title"];
+ string contentType = response.Headers["Content-Type"];
+ Console.WriteLine($"Object keyName: {response.Key}");
+ Console.WriteLine($"Object metadata, Title: {title}");
+ Console.WriteLine($"Content type: {contentType}");
+
+ // Retrieve the contents of the file.
+ responseBody = reader.ReadToEnd();
+
+ // Write the contents of the file to disk.
+ string filePath = $"C:\\Temp\\copy_of_{keyName}";
+ }
+ }
+ catch (AmazonS3Exception e)
+ {
+ // If the bucket or the object do not exist
+ Console.WriteLine($"Error: '{e.Message}'");
+ }
+ }
+ #endregion
}
-}
\ No newline at end of file
+}
diff --git a/S3Demo/Run.cs b/S3Demo/Run.cs
index f462e38..747cb8e 100644
--- a/S3Demo/Run.cs
+++ b/S3Demo/Run.cs
@@ -2,6 +2,8 @@
using Amazon.Runtime;
using Amazon.S3;
using System;
+using S3Demo.Stack;
+using S3Demo.Model;
using System.Threading.Tasks;
namespace S3Demo
@@ -27,22 +29,38 @@ namespace S3Demo
{
try
{
- //uploaddir
+ //upload Dir
string dirPath = @"C:\Users\song\Pictures\Saved Pictures\";
string suffix = "*.png";
//await UploadDirMPUHighLevelAPI.Uploaderdir(bucketName, dirPath, suffix, credentials, conf);
- //uploadfile
- string keyName = "360.png";
+ //upload File
+ string keyName = "logo.png";
//await UploadFileMPUHighLevelAPI.Uploaderfile(bucketName, keyName, dirPath, credentials, conf);
- //objectTag
+ //list Objects
+ //await ListObjects.Listingobjects(bucketName, credentials, conf);
+
+ //upload Object With Tag
string filaPath = @"C:\Users\song\Pictures\Saved Pictures\" + keyName;
String[] tags = new String[] { "SN123456", DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:sszzz"), "AB023" };
- await ObjectTag.Putobjectswithtags(bucketName, keyName, filaPath, tags, credentials, conf);
+ //await ObjectModel.Putobjectswithtags(bucketName, keyName, filaPath, tags, credentials, conf);
+
+ //read Object
+ await ObjectModel.Readobjectdata(bucketName, keyName, credentials, conf);
- //getObject
- //await GetObject.Readobjectdata(bucketName, keyName, credentials, conf);
+ //isExist Bucket
+ //从"minio/test"中取出存储桶名
+ var _bucketName = bucketName.Split('/')[1];
+ var flag = await BucketModel.isExistBucket(_bucketName, credentials, conf);
+ if (flag)
+ {
+ Console.WriteLine("exist");
+ }
+ else
+ {
+ Console.WriteLine("not exist");
+ }
}
catch (Exception ex)
{
diff --git a/S3Demo/S3Demo.csproj b/S3Demo/S3Demo.csproj
index d80d228..db72030 100644
--- a/S3Demo/S3Demo.csproj
+++ b/S3Demo/S3Demo.csproj
@@ -13,7 +13,7 @@
true
- preview
+ preview
AnyCPU
@@ -51,12 +51,13 @@
-
-
+
+
+
-
-
+
+
diff --git a/S3Demo/Stack/ListObjects.cs b/S3Demo/Stack/ListObjects.cs
new file mode 100644
index 0000000..f853165
--- /dev/null
+++ b/S3Demo/Stack/ListObjects.cs
@@ -0,0 +1,72 @@
+// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+///
+/// List the objects in an Amazon Simple Storage Service (Amazon S3) bucket.
+/// The example was created using the AWS SDK for .NET version 3.7 and
+/// .NET Core 5.0.
+///
+namespace S3Demo.Stack
+{
+ // snippet-start:[S3.dotNET.ListObjectsExample]
+ using System;
+ using System.Threading.Tasks;
+ using Amazon.Runtime;
+ using Amazon.S3;
+ using Amazon.S3.Model;
+
+ public class ListObjects
+ {
+ public static async Task Listingobjects(string bucketName, BasicAWSCredentials credentials, AmazonS3Config conf)
+ {
+ //string bucketName = "doc-example-bucket";
+ IAmazonS3 client;
+
+ using (client = new AmazonS3Client(credentials, conf))
+ {
+ Console.WriteLine($"Listing objects stored in the bucket {bucketName}.");
+ await ListingObjectsAsync(client, bucketName);
+ }
+ }
+
+ ///
+ /// Uses the client object to get a list of the objects in the Amazon
+ /// S3 bucket in the bucketName parameter.
+ ///
+ /// The initialized S3 client obect used to call
+ /// the ListObjectsAsync method.
+ /// The bucket name for which you want to
+ /// retrieve a list of objects.
+ public static async Task ListingObjectsAsync(IAmazonS3 client, string bucketName)
+ {
+ try
+ {
+ ListObjectsV2Request request = new()
+ {
+ BucketName = bucketName,
+ MaxKeys = 5,
+ };
+
+ var response = new ListObjectsV2Response();
+
+ do
+ {
+ response = await client.ListObjectsV2Async(request);
+
+ response.S3Objects
+ .ForEach(obj => Console.WriteLine($"{obj.Key,-35}{obj.LastModified.ToShortDateString(),10}{obj.Size,10}"));
+
+ // If the response is truncated, set the request ContinuationToken
+ // from the NextContinuationToken property of the response.
+ request.ContinuationToken = response.NextContinuationToken;
+ } while (response.IsTruncated);
+ }
+ catch (AmazonS3Exception ex)
+ {
+ Console.WriteLine($"Error encountered on server. Message:'{ex.Message}' getting list of objects.");
+ }
+ }
+ }
+
+ // snippet-end:[S3.dotNET.ListObjectsExample]
+}
diff --git a/S3Demo/UploadDirMPUHighLevelAPI.cs b/S3Demo/Stack/UploadDirMPUHighLevelAPI.cs
similarity index 99%
rename from S3Demo/UploadDirMPUHighLevelAPI.cs
rename to S3Demo/Stack/UploadDirMPUHighLevelAPI.cs
index 4e9c446..6616279 100644
--- a/S3Demo/UploadDirMPUHighLevelAPI.cs
+++ b/S3Demo/Stack/UploadDirMPUHighLevelAPI.cs
@@ -7,7 +7,7 @@ using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Transfer;
-namespace S3Demo
+namespace S3Demo.Stack
{
///
/// This example uses the Amazon Simple Storage Service (Amazon S3)
diff --git a/S3Demo/UploadFileMPUHighLevelAPI.cs b/S3Demo/Stack/UploadFileMPUHighLevelAPI.cs
similarity index 99%
rename from S3Demo/UploadFileMPUHighLevelAPI.cs
rename to S3Demo/Stack/UploadFileMPUHighLevelAPI.cs
index 818ddaf..8d507d0 100644
--- a/S3Demo/UploadFileMPUHighLevelAPI.cs
+++ b/S3Demo/Stack/UploadFileMPUHighLevelAPI.cs
@@ -7,7 +7,7 @@ using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Transfer;
-namespace S3Demo
+namespace S3Demo.Stack
{
///
/// This example shows how to use the TransferUtility api for Amazon Simple