0

ここに私が作成した簡単なクラスがあります:

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

4

0 に答える 0