You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
7.5 KiB

3 years ago
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 years ago
// SPDX - License - Identifier: Apache - 2.0
3 years ago
3 years ago
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
3 years ago
{
3 years ago
class ObjectModel
3 years ago
{
3 years ago
#region 发送对象(带标签)
3 years ago
public static async Task Putobjectswithtags(string bucketName, string keyName, string filePath, string[] tags, BasicAWSCredentials credentials, AmazonS3Config conf)
{
//string bucketName = "doc-example-bucket";
//string keyName = "newobject.txt";
//string filePath = @"*** file path ***";
// Specify your bucket region (an example region is shown).
//RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
var client = new AmazonS3Client(credentials, conf);
await PutObjectsWithTagsAsync(client, bucketName, keyName, filePath, tags);
}
/// <summary>
/// This method uploads an object with tags. It then shows the tag
/// values, changes the tags, and shows the new tags.
/// </summary>
/// <param name="client">The Initialized Amazon S3 client object used
/// to call the methods to create and change an objects tags.</param>
/// <param name="bucketName">A string representing the name of the
/// bucket where the object will be stored.</param>
/// <param name="keyName">A string representing the key name of the
/// object to be tagged.</param>
/// <param name="filePath">The directory location and file name of the
/// object to be uploaded to the S3 bucket.</param>
public static async Task PutObjectsWithTagsAsync(IAmazonS3 client, string bucketName, string keyName, string filePath, string[] tags)
{
try
{
// Create an object with tags.
var putRequest = new PutObjectRequest
{
BucketName = bucketName,
Key = keyName,
FilePath = filePath,
TagSet = new List<Tag>
{
new Tag { Key = "sn", Value = tags[0] },
new Tag { Key = "date", Value = tags[1] },
new Tag { Key = "code", Value = tags[2] }
},
};
PutObjectResponse response = await client.PutObjectAsync(putRequest);
// Now retrieve the new object's tags.
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}"));
/*
// Replace the tagset new tags.
Tagging newTagSet = new()
{
TagSet = new List<Tag>
{
new Tag { Key = "Key3", Value = "Value3" },
new Tag { Key = "Key4", Value = "Value4" },
},
};
PutObjectTaggingRequest putObjTagsRequest = new()
{
BucketName = bucketName,
Key = keyName,
Tagging = newTagSet,
};
PutObjectTaggingResponse response2 = await client.PutObjectTaggingAsync(putObjTagsRequest);
// Retrieve the tags again and show the values.
GetObjectTaggingRequest getTagsRequest2 = new()
{
BucketName = bucketName,
Key = keyName,
};
GetObjectTaggingResponse objectTags2 = await client.GetObjectTaggingAsync(getTagsRequest2);
objectTags2.Tagging
.ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}"));*/
}
catch (AmazonS3Exception ex)
{
Console.WriteLine(
$"Error: '{ex.Message}'");
}
}
3 years ago
#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);
}
/// <summary>
/// This method copies the contents of the object keyName to another
/// location, for example, to your local system.
/// </summary>
/// <param name="client">The initialize S3 client used to call
/// GetObjectAsync.</param>
/// <param name="bucketName">The name of the S3 bucket which contains
/// the object to copy.</param>
/// <param name="keyName">The name of the object you want to copy.</param>
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
3 years ago
}
3 years ago
}