这是我创建的一个简单的类:
public delegate void ChangeEventHandler(object sender, UploadEventArgs e);
public class UploadEventArgs : EventArgs
{
public string FileName;
public string OldFileName;
//public string ThumbnailName;
public string ContentType;
public int ContentLength;
}
public class Bucket
{
#region Events
public event ChangeEventHandler OnStart;
public event ChangeEventHandler OnComplete;
public event ChangeEventHandler OnError;
#endregion
#region Fields
private string _key;
private string _secret;
private const string _seviceUrl = "s3-eu-west-1.amazonaws.com";
#endregion
#region Properties
public string BucketName { get; set; }
#endregion
#region Constructors
public Bucket(string key, string secret)
{
this._key = key;
this._secret = secret;
}
public Bucket(string key, string secret, string bucketName)
{
this._key = key;
this._secret = secret;
this.BucketName = bucketName;
}
#endregion
#region Public methods
/// <summary>
/// Creates a bucket in the default EUWest1 region endpoint
/// </summary>
/// <param name="bucketName">The name of the new bucket</param>
public void Create(string bucketName)
{
Create(bucketName, RegionEndpoint.EUWest1);
}
/// <summary>
/// Creates a bucket in the specified region endpoint
/// </summary>
/// <param name="bucketName">The name of the new bucket</param>
/// <param name="region">The region endpoint of the bucket</param>
public void Create(string bucketName, RegionEndpoint region)
{
using (var client = Amazon.AWSClientFactory.CreateAmazonS3Client(region))
{
// Creates the bucket.
S3DirectoryInfo rootDirectory = new S3DirectoryInfo(client, bucketName);
rootDirectory.Create();
}
this.BucketName = bucketName; // Set our bucketname property
}
public string UploadMultipartFile(HttpPostedFile file)
{
if (string.IsNullOrEmpty(BucketName))
throw new Exception("The bucketname must be supplied."); // Throw an exception if the bucketname has not been initiated
string keyName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
UploadEventArgs e = new UploadEventArgs()
{
FileName = keyName,
OldFileName = file.FileName,
ContentType = file.ContentType,
ContentLength = file.ContentLength
};
try
{
OnStart(this, e);
}
catch (Exception ex)
{
}
var config = new AmazonS3Config()
{
ServiceURL = _seviceUrl
};
AmazonS3 client = new AmazonS3Client(_key, _secret, config);
// List to store upload part responses.
List<UploadPartResponse> uploadResponses =
new List<UploadPartResponse>();
// 1. Initialize.
InitiateMultipartUploadRequest initiateRequest =
new InitiateMultipartUploadRequest()
.WithBucketName(BucketName)
.WithKey(keyName);
InitiateMultipartUploadResponse initResponse =
client.InitiateMultipartUpload(initiateRequest);
// 2. Upload Parts.
long contentLength = file.InputStream.Length;
long partSize = 5 * (long)Math.Pow(2, 20); // 5 MB
try
{
long filePosition = 0;
for (int i = 1; filePosition < contentLength; i++)
{
// Create request to upload a part.
var uploadRequest = new UploadPartRequest()
.WithBucketName(BucketName)
.WithKey(keyName)
.WithUploadId(initResponse.UploadId)
.WithPartNumber(i)
.WithPartSize(partSize)
.WithFilePosition(filePosition)
.WithInputStream(file.InputStream);
// Upload part and add response to our list.
uploadResponses.Add(client.UploadPart((UploadPartRequest)uploadRequest));
filePosition += partSize;
}
// Step 3: complete.
CompleteMultipartUploadRequest completeRequest =
new CompleteMultipartUploadRequest()
.WithBucketName(BucketName)
.WithKey(keyName)
.WithUploadId(initResponse.UploadId)
.WithPartETags(uploadResponses);
CompleteMultipartUploadResponse completeUploadResponse =
client.CompleteMultipartUpload(completeRequest);
OnComplete(this, e);
}
catch (Exception exception)
{
Console.WriteLine("Exception occurred: {0}", exception.Message);
client.AbortMultipartUpload(new AbortMultipartUploadRequest()
.WithBucketName(BucketName)
.WithKey(keyName)
.WithUploadId(initResponse.UploadId));
OnError(this, e);
}
return keyName;
}
#endregion
}
我遇到的问题是,当调用“OnStart”时,我收到一条错误,指出“对象引用未设置为对象的实例。”。 我已将委托更改为只将“UploadEventArgs”作为参数,我遇到了同样的问题。 我注释掉了所有未使用的属性,但我仍然收到错误。
我知道这是非常简单的事情,但我看不到木头的树木......
任何帮助将不胜感激...... / r3plica