本文整理汇总了C#中Amazon.S3.AmazonS3Client.ListObjects方法的典型用法代码示例。如果您正苦于以下问题:C# AmazonS3Client.ListObjects方法的具体用法?C# AmazonS3Client.ListObjects怎么用?C# AmazonS3Client.ListObjects使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Amazon.S3.AmazonS3Client
的用法示例。
在下文中一共展示了AmazonS3Client.ListObjects方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RefreshAssetList
public void RefreshAssetList()
{
string SecretKey = null;
string PublicKey = null;
AmazonS3Client Client = new AmazonS3Client(PublicKey, SecretKey);
ListObjectsRequest Request = new ListObjectsRequest
{
BucketName = "assets.minecraft.net",
};
ListObjectsResponse Result;
List<Release> releases = new List<Release>();
do
{
Result = Client.ListObjects(Request);
foreach (S3Object o in Result.S3Objects)
{
string IsSnapshot = "Release";
if(!o.Key.Contains("minecraft.jar")) continue;
if (Regex.IsMatch(o.Key, "[0-9][0-9]w[0-9][0-9]")) IsSnapshot = "Snapshot";
else if (o.Key.Contains("pre")) IsSnapshot = "Pre-release";
releases.Add(new Release { Version = o.Key.Split('/')[0],
Size = (o.Size / 1024).ToString() + "KB",
Uploaded = DateTime.Parse(o.LastModified),
Type = IsSnapshot,
Key = o.Key} );
}
}
while (Result.IsTruncated);
releases.Sort(new Comparison<Release>((x, y) => DateTime.Compare(y.Uploaded, x.Uploaded)));
_Releases.Clear();
foreach (Release r in releases)
{
_Releases.Add(r);
}
Client.Dispose();
Result.Dispose();
}
示例2: Main
static void Main()
{
// Connect to Amazon S3 service with authentication
BasicAWSCredentials basicCredentials =
new BasicAWSCredentials("AKIAIIYG27E27PLQ6EWQ",
"hr9+5JrS95zA5U9C6OmNji+ZOTR+w3vIXbWr3/td");
AmazonS3Client s3Client = new AmazonS3Client(basicCredentials);
// Display all S3 buckets
ListBucketsResponse buckets = s3Client.ListBuckets();
foreach (var bucket in buckets.Buckets)
{
Console.WriteLine(bucket.BucketName);
}
// Display and download the files in the first S3 bucket
string bucketName = buckets.Buckets[0].BucketName;
Console.WriteLine("Objects in bucket '{0}':", bucketName);
ListObjectsResponse objects =
s3Client.ListObjects(new ListObjectsRequest() { BucketName = bucketName });
foreach (var s3Object in objects.S3Objects)
{
Console.WriteLine("\t{0} ({1})", s3Object.Key, s3Object.Size);
if (s3Object.Size > 0)
{
// We have a file (not a directory) --> download it
GetObjectResponse objData = s3Client.GetObject(
new GetObjectRequest() { BucketName = bucketName, Key = s3Object.Key });
string s3FileName = new FileInfo(s3Object.Key).Name;
SaveStreamToFile(objData.ResponseStream, s3FileName);
}
}
// Create a new directory and upload a file in it
string path = "uploads/new_folder_" + DateTime.Now.Ticks;
string newFileName = "example.txt";
string fullFileName = path + "/" + newFileName;
string fileContents = "This is an example file created through the Amazon S3 API.";
s3Client.PutObject(new PutObjectRequest() {
BucketName = bucketName,
Key = fullFileName,
ContentBody = fileContents}
);
Console.WriteLine("Created a file in Amazon S3: {0}", fullFileName);
// Share the uploaded file and get a download URL
string uploadedFileUrl = s3Client.GetPreSignedURL(new GetPreSignedUrlRequest()
{
BucketName = bucketName,
Key = fullFileName,
Expires = DateTime.Now.AddYears(5)
});
Console.WriteLine("File download URL: {0}", uploadedFileUrl);
System.Diagnostics.Process.Start(uploadedFileUrl);
}
示例3: ListFiles
public static IEnumerable<string> ListFiles(string bucketName, string accessKeyID, string secretAccessKey)
{
List<string> fileNames = new List<string>();
try
{
var s3Client = new AmazonS3Client(accessKeyID, secretAccessKey, Amazon.RegionEndpoint.USEast1);
ListObjectsRequest request = new ListObjectsRequest
{
BucketName = bucketName
};
while (request != null)
{
ListObjectsResponse response = s3Client.ListObjects(request);
foreach (S3Object entry in response.S3Objects)
{
fileNames.Add(entry.Key);
}
if (response.IsTruncated)
{
request.Marker = response.NextMarker;
}
else
{
request = null;
}
}
}
catch (Exception e)
{
}
return fileNames;
}
示例4: TestSerializingExceptions
public void TestSerializingExceptions()
{
using(var client = new Amazon.S3.AmazonS3Client())
{
try
{
var fakeBucketName = "super.duper.fake.bucket.name.123." + Guid.NewGuid().ToString();
client.ListObjects(fakeBucketName);
}
catch(AmazonS3Exception e)
{
TestException(e);
}
var s3pue = CreateS3PostUploadException();
TestException(s3pue);
var doe = CreateDeleteObjectsException();
TestException(doe);
var aace = new AdfsAuthenticationControllerException("Message");
TestException(aace);
#pragma warning disable 618
var ccre = new CredentialCallbackRequiredException("Message");
TestException(ccre);
var afe = new AuthenticationFailedException("Message");
TestException(afe);
#pragma warning restore 618
}
}
示例5: SoftDeleteFolders
public static void SoftDeleteFolders(ControllerConfiguration context, IEnumerable<string> folders)
{
if (context == null)
throw new ArgumentNullException("context", "Context cannot be null.");
if (folders == null)
throw new ArgumentNullException("folders", "Folders cannot be null.");
using (var client = new AmazonS3Client(context.AwsAccessKeyId, context.AwsSecretAccessKey))
{
foreach (var folder in folders)
{
int maxResults = 100;
int lastCount = maxResults;
while (maxResults == lastCount)
{
using (var listResponse = client.ListObjects(new ListObjectsRequest()
{
BucketName = context.BucketName,
Prefix = folder,
}))
{
lastCount = listResponse.S3Objects.Count;
Parallel.ForEach(listResponse.S3Objects, folderObject =>
{
using (var copyResponse = client.CopyObject(new CopyObjectRequest()
{
SourceBucket = context.BucketName,
DestinationBucket = context.BucketName,
SourceKey = folderObject.Key,
DestinationKey = ".recycled/" + folderObject.Key,
})) { }
});
Parallel.ForEach(listResponse.S3Objects, folderObject =>
{
using (var deleteReponse = client.DeleteObject(new DeleteObjectRequest()
{
BucketName = context.BucketName,
Key = folderObject.Key,
})) { }
});
}
}
}
}
}
示例6: DeleteAllBucketItems
public static void DeleteAllBucketItems(string bucketName)
{
using (var client = new AmazonS3Client(Settings.AccessKey, Settings.Secret))
{
var request = new ListObjectsRequest
{
BucketName = bucketName
};
var response = client.ListObjects(request);
foreach (var entry in response.S3Objects)
{
client.DeleteObject(bucketName, entry.Key);
}
}
}
示例7: S3DeleteItemsFolder
public void S3DeleteItemsFolder(string bucketName, string serverFolder)
{
//ref: http://docs.aws.amazon.com/AmazonS3/latest/dev/RetrievingObjectUsingNetSDK.html
int count = 0;
using (var client = new AmazonS3Client(this.AcesssKey, this.SecretKey, this.Region))
{
ListObjectsRequest request = new ListObjectsRequest
{
BucketName = bucketName,
MaxKeys = 10,
Prefix = serverFolder
};
do
{
ListObjectsResponse response = client.ListObjects(request);
// Process response
foreach (S3Object entry in response.S3Objects)
{
if (entry.Key == serverFolder || entry.Key == string.Format("{0}/", serverFolder) || entry.Key == string.Format("/{0}", serverFolder))
continue; //Folder
count++;
//System.Diagnostics.Debug.WriteLine(string.Format("AwsS3 -- key = {0} size = {1} / {2} items read", entry.Key, entry.Size.ToString("#,##0"), count.ToString("#,##0")));
this.S3DeleteItem(bucketName, entry.Key);
Console.WriteLine(string.Format("{0} -- {1} items deleted", entry.Key, count.ToString("#,##0")));
}
// If response is truncated, set the marker to get the next
// set of keys.
if (response.IsTruncated)
{
request.Marker = response.NextMarker;
}
else
{
request = null;
}
} while (request != null);
}
}
示例8: DeleteBucket
public virtual void DeleteBucket(AmazonS3Client s3Client, string bucketName)
{
// First, try to delete the bucket.
var deleteBucketRequest = new DeleteBucketRequest
{
BucketName = bucketName
};
try
{
s3Client.DeleteBucket(deleteBucketRequest);
// If we get here, no error was generated so we'll assume the bucket was deleted and return.
return;
}
catch (AmazonS3Exception ex)
{
if (!ex.ErrorCode.Equals("BucketNotEmpty"))
{
// We got an unanticipated error. Just rethrow.
throw;
}
}
// If we got here, then our bucket isn't empty so we need to delete the items in it first.
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest {BucketName = bucketName};
foreach (S3Object obj in s3Client.ListObjects(new ListObjectsRequest {BucketName = bucketName}).S3Objects)
{
// Add keys for the objects to the delete request
deleteObjectsRequest.AddKey(obj.Key, null);
}
// Submit the request
s3Client.DeleteObjects(deleteObjectsRequest);
// The bucket is empty now, so delete the bucket.
s3Client.DeleteBucket(deleteBucketRequest);
}
示例9: DownloadFolder
private void DownloadFolder(string folder, Label label)
{
var region = Amazon.RegionEndpoint.USEast1;
var credentials = new BasicAWSCredentials("AKIAI6FLMSLBSLE4YBKQ", "l3sIAyIAvSqsg+uWbwmnD8MgHoaDeDHs2tOyVbYT");
S3Client = new AmazonS3Client(credentials, region);
var count = 0;
label.Text = count.ToString();
var request = new ListObjectsRequest
{
BucketName = "logs-concierge-prod",
Prefix = folder,
MaxKeys = 1000
};
var response = S3Client.ListObjects(request);
while (response.S3Objects.Count > 0)
{
count += response.S3Objects.Count;
label.Text = count.ToString();
response = S3Client.ListObjects(request);
var lines = new List<string>();
var startPoint = "";
foreach (var item in response.S3Objects)
{
lines.Add(String.Format("{0}\t{1}\t{2}", item.Key, item.Size, item.LastModified));
startPoint = item.Key;
}
request.Marker = startPoint;
WriteToFile(lines, folder);
}
label.Text = count.ToString();
}
示例10: FileExists
public bool FileExists(string path, string bucket)
{
using (var s3 = new AmazonS3Client(_connectionInfo.AccessKey, _connectionInfo.SecretKey,
new AmazonS3Config {ServiceURL = "http://s3.amazonaws.com"}))
{
return s3.ListObjects(new ListObjectsRequest
{
BucketName = bucket,
Prefix = path,
MaxKeys = 1
})
.MaxKeys > 0;
}
}
示例11: executeSomeBucketOperations
private void executeSomeBucketOperations(AmazonS3Config s3Config)
{
using (var s3Client = new AmazonS3Client(s3Config))
{
// Call ListBuckets first to verify that AmazonS3PostMarshallHandler.ProcessRequestHandlers
// correctly computes the endpoint when no bucket name is present.
var listBucketsResponse = s3Client.ListBuckets();
Assert.IsNotNull(listBucketsResponse);
Assert.IsFalse(string.IsNullOrEmpty(listBucketsResponse.ResponseMetadata.RequestId));
// Bonus call on ListObjects if we can find a bucket compatible with the test region (to avoid 301
// errors due to addressing bucket on wrong endpoint). This verifies that
// AmazonS3PostMarshallHandler.ProcessRequestHandlers correctly computes the endpoint when
// a bucket name is present.
string bucketName = null;
foreach (var bucket in listBucketsResponse.Buckets)
{
try
{
var bucketLocationResponse = s3Client.GetBucketLocation(bucket.BucketName);
if (string.IsNullOrEmpty(bucketLocationResponse.Location) && s3Config.RegionEndpoint == RegionEndpoint.USEast1)
bucketName = bucket.BucketName;
else if (string.Equals(s3Config.RegionEndpoint.SystemName, bucketLocationResponse.Location, StringComparison.OrdinalIgnoreCase))
bucketName = bucket.BucketName;
if (!string.IsNullOrEmpty(bucketName))
break;
}
catch(AmazonS3Exception e)
{
if (e.StatusCode != System.Net.HttpStatusCode.NotFound)
throw;
}
}
if (!string.IsNullOrEmpty(bucketName))
{
var listObjectsResponse = s3Client.ListObjects(new ListObjectsRequest { BucketName = bucketName });
Assert.IsNotNull(listObjectsResponse);
Assert.IsNotNull(listObjectsResponse.ResponseMetadata);
}
}
}
示例12: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (this.Session[Settings.Default.FlashSessionKey] != null)
{
this.FlashLiteralWrapper.Visible = true;
this.FlashLiteral.Text = this.Session[Settings.Default.FlashSessionKey].ToString();
this.Session[Settings.Default.FlashSessionKey] = null;
}
else
{
this.FlashLiteralWrapper.Visible = false;
}
this._petIdString = this.Request.QueryString["petid"];
if (String.IsNullOrEmpty(this._petIdString))
{
this.StatsLiteral.Text = "Add a New Pet";
this.SaveStatsButton.Text = "Save New Pet";
}
else
{
this.PhotoPanel.Visible = true;
}
this._userBucketName = String.Format(Settings.Default.BucketNameFormat, this.Context.User.Identity.Name, this._petIdString);
this._itemName = this._petIdString ?? Guid.NewGuid().ToString();
this._domainName = String.Format(Settings.Default.SimpleDbDomainNameFormat, this.Context.User.Identity.Name);
if (!this.Page.IsPostBack)
{
List<int> years = new List<int>(100);
for (int i = 0; i < 100; i++)
{
years.Add(DateTime.Now.AddYears(i * -1).Year);
}
this.YearDropDownList.DataSource = years.OrderByDescending(y => y);
this.YearDropDownList.DataBind();
this.SelectMonth();
this.SelectDay();
Pet pet = default(Pet);
List<string> files = new List<string>();
if (!String.IsNullOrEmpty(this._petIdString))
{
//
// Try to get the requested pet from the user's private domain
//
DomainHelper.CheckForDomain(this._domainName, _simpleDBClient);
GetAttributesRequest getAttributeRequest = new GetAttributesRequest()
.WithDomainName(this._domainName)
.WithItemName(this._itemName);
GetAttributesResponse getAttributeResponse = _simpleDBClient.GetAttributes(getAttributeRequest);
List<Attribute> attrs = null;
bool showPublic = false;
if (getAttributeResponse.IsSetGetAttributesResult())
{
attrs = getAttributeResponse.GetAttributesResult.Attribute;
showPublic = false;
//
// If we can't find it try the public domain
//
if (attrs.Count == 0)
{
showPublic = true;
}
}
if (showPublic)
{
Response.Redirect(String.Concat("PetProfile.aspx?petid", _petIdString));
return;
}
pet = new Pet
{
Name = attrs.First(a => a.Name == "Name").Value,
Birthdate = attrs.First(a => a.Name == "Birthdate").Value,
Sex = attrs.First(a => a.Name == "Sex").Value,
Type = attrs.First(a => a.Name == "Type").Value,
Breed = attrs.First(a => a.Name == "Breed").Value,
Likes = attrs.First(a => a.Name == "Likes").Value,
Dislikes = attrs.First(a => a.Name == "Dislikes").Value
};
this.Public.Checked = bool.Parse(attrs.First(a => a.Name == "Public").Value);
using (AmazonS3Client s3Client = new AmazonS3Client(Settings.Default.AWSAccessKey.Trim(), Settings.Default.AWSSecretAccessKey.Trim()))
{
BucketHelper.CheckForBucket(this._petIdString, s3Client);
ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
.WithBucketName(this._userBucketName);
using (ListObjectsResponse listObjectsResponse = s3Client.ListObjects(listObjectsRequest))
{
files = listObjectsResponse.S3Objects.Select(o => String.Format(Settings.Default.S3BucketUrlFormat, this._userBucketName, o.Key)).ToList();
string firstPhoto = files.FirstOrDefault();
//.........这里部分代码省略.........
示例13: ListFromBucket
public static IEnumerable<string> ListFromBucket(string bucketName)
{
using (var client = new AmazonS3Client(Settings.AccessKey, Settings.Secret))
{
var request = new ListObjectsRequest
{
BucketName = bucketName
};
var response = client.ListObjects(request);
foreach (var entry in response.S3Objects)
{
yield return entry.Key;
}
}
}
示例14: GetBucketItemSizeInBytes
public static long GetBucketItemSizeInBytes(string bucketName, string key)
{
using (var client = new AmazonS3Client(Settings.AccessKey, Settings.Secret))
{
var request = new ListObjectsRequest
{
BucketName = bucketName,
Prefix = key
};
var response = client.ListObjects(request).S3Objects.FirstOrDefault();
if (response == null) return 0;
return response.Size;
}
}
示例15: FailureRetryRequests
private static void FailureRetryRequests(int totalRequests, int retryRequests, int extraRequests, AmazonS3Client client)
{
for (int i = 0; i < totalRequests; i++)
{
try
{
var response = client.ListObjects("CapacityManagerTests");
}
catch (Exception)
{
if (i == totalRequests - 1)
{
Assert.AreEqual(retryRequests * 2, requestCount - extraRequests);
}
continue;
}
}
}