Exemple #1
0
        public static void TestPutObjectAsync()
        {
            // Create a client
            AmazonS3Client client = new AmazonS3Client();

            PutObjectResponse response;
            IAsyncResult      asyncResult;

            //
            // Create a PutObject request
            //
            // You will need to change the BucketName below in order to run this
            // sample code.
            //
            PutObjectRequest request = new PutObjectRequest
            {
                BucketName  = "PUT-YOUR-OWN-EXISTING-BUCKET-NAME-HERE",
                Key         = "Item",
                ContentBody = "This is sample content..."
            };

            response = client.PutObject(request);
            Console.WriteLine("Finished PutObject operation for {0}.", request.Key);
            Console.WriteLine("Service Response:");
            Console.WriteLine("-----------------");
            Console.WriteLine("{0}", response);
            Console.Write("\n\n");

            request.Key = "Item1";
            asyncResult = client.BeginPutObject(request, null, null);
            while (!asyncResult.IsCompleted)
            {
                //
                // Do some work here
                //
            }
            try {
                response = client.EndPutObject(asyncResult);
            }
            catch (AmazonS3Exception s3Exception) {
                //
                // Code to process exception
                //
            }

            Console.WriteLine("Finished Async PutObject operation for {0}.", request.Key);
            Console.WriteLine("Service Response:");
            Console.WriteLine("-----------------");
            Console.WriteLine(response);
            Console.Write("\n\n");

            request.Key = "Item2";
            asyncResult = client.BeginPutObject(request, SimpleCallback, null);

            request.Key = "Item3";
            asyncResult = client.BeginPutObject(request, CallbackWithClient, client);

            request.Key = "Item4";
            asyncResult = client.BeginPutObject(request, CallbackWithState,
                                                new ClientState {
                Client = client, Start = DateTime.Now
            });

            Thread.Sleep(TimeSpan.FromSeconds(5));
        }
Exemple #2
0
        // snippet-end:[s3.dotnet.putobject.async.callbackwithstate]

        //
        // Function TestPutObjectAsync().
        // Test synchronous and asynchronous variations of PutObject().
        //
        // snippet-start:[s3.dotnet.putobject.async.testputobjectasync.start]
        public static void TestPutObjectAsync(string bucket)
        {
            // Create a client
            AmazonS3Client client = new AmazonS3Client();

            PutObjectResponse response;
            IAsyncResult      asyncResult;

            //
            // Create a PutObject request object using the supplied bucket name.
            //
            PutObjectRequest request = new PutObjectRequest
            {
                BucketName  = bucket,
                Key         = "Item0-Synchronous",
                ContentBody = "Put S3 object synchronously."
            };

            // snippet-end:[s3.dotnet.putobject.async.testputobjectasync.start]

            //
            // Perform a synchronous PutObject operation.
            //
            Console.WriteLine("-------------------------------------------------------------------------------");
            Console.WriteLine("Performing synchronous PutObject operation for {0}.", request.Key);
            response = client.PutObject(request);
            Console.WriteLine("Finished PutObject operation for {0}.", request.Key);
            Console.WriteLine("Service Response:");
            Console.WriteLine("-----------------");
            Console.WriteLine("Request ID: {0}", response.ResponseMetadata.RequestId);
            Console.Write("\n");

            //
            // Perform an async PutObject operation and wait for the response.
            //
            // (Re-use the existing PutObject request object since it isn't being used for another async request.)
            //
            request.Key         = "Item1-Async-wait";
            request.ContentBody = "Put S3 object asynchronously; wait for response.";
            Console.WriteLine("-------------------------------------------------------------------------------");
            Console.WriteLine("Performing async PutObject operation and waiting for response (Key: {0}).", request.Key);

            // snippet-start:[s3.dotnet.putobject.async.testputobjectasync.beginputobject]
            asyncResult = client.BeginPutObject(request, null, null);
            while (!asyncResult.IsCompleted)
            {
                //
                // Do some work here
                //
            }
            try
            {
                response = client.EndPutObject(asyncResult);
            }
            catch (AmazonS3Exception s3Exception)
            {
                Console.WriteLine("Caught exception calling EndPutObject:");
                Console.WriteLine(s3Exception);
            }
            // snippet-end:[s3.dotnet.putobject.async.testputobjectasync.beginputobject]

            Console.WriteLine("Finished Async PutObject operation for {0}.", request.Key);
            Console.WriteLine("Service Response:");
            Console.WriteLine("-----------------");
            Console.WriteLine("Request ID: {0}\n", response.ResponseMetadata.RequestId);

            Console.WriteLine("-------------------------------------------------------------------------------");
            Console.WriteLine("Performing the following async PutObject operations:");
            Console.WriteLine("\"simple callback\", \"callback with client\", and \"callback with state\"...\n");

            //
            // Perform an async PutObject operation with a simple callback.
            //
            // (Re-use the existing PutObject request object since it isn't being used for another async request.)
            //
            request.Key         = "Item2-Async-simple";
            request.ContentBody = "Put S3 object asynchronously; use simple callback.";

            Console.WriteLine("PutObject with simple callback (Key: {0}).", request.Key);
            // snippet-start:[s3.dotnet.putobject.async.testputobjectasync.simplecallback]
            asyncResult = client.BeginPutObject(request, SimpleCallback, null);
            // snippet-end:[s3.dotnet.putobject.async.testputobjectasync.simplecallback]

            //
            // Perform an async PutObject operation with a client callback.
            //
            // Create a PutObject request object for this call using the supplied bucket name.
            //
            PutObjectRequest request_client = new PutObjectRequest
            {
                BucketName  = bucket,
                Key         = "Item3-Async-client",
                ContentBody = "Put S3 object asynchronously; use callback with client."
            };

            Console.WriteLine("PutObject with client callback (Key: {0}).", request_client.Key);
            // snippet-start:[s3.dotnet.putobject.async.testputobjectasync.callbackwithclient]
            asyncResult = client.BeginPutObject(request_client, CallbackWithClient, client);
            // snippet-end:[s3.dotnet.putobject.async.testputobjectasync.callbackwithclient]

            //
            // Perform an async PutObject operation with a state callback.
            //
            // Create a PutObject request object for this call using the supplied bucket name.
            //
            PutObjectRequest request_state = new PutObjectRequest
            {
                BucketName  = bucket,
                Key         = "Item3-Async-state",
                ContentBody = "Put S3 object asynchronously; use callback with state."
            };

            Console.WriteLine("PutObject with state callback (Key: {0}).\n", request_state.Key);
            // snippet-start:[s3.dotnet.putobject.async.testputobjectasync.callbackwithstate]
            asyncResult = client.BeginPutObject(request_state, CallbackWithState,
                                                new ClientState {
                Client = client, Start = DateTime.Now
            });
            // snippet-end:[s3.dotnet.putobject.async.testputobjectasync.callbackwithstate]

            //
            // Finished with async calls. Wait a bit for them to finish.
            //
            Thread.Sleep(TimeSpan.FromSeconds(5));
        }
        // snippet-end:[s3.dotnet.putobject.async.callbackwithstate]

        // snippet-start:[s3.dotnet.putobject.async.testputobjectasync.start]
        public static void TestPutObjectAsync(string bucket)
        {
            // Create a client
            AmazonS3Client client = new AmazonS3Client();

            PutObjectResponse response;
            IAsyncResult      asyncResult;

            //
            // Create a PutObject request
            //
            // You will need to change the BucketName below in order to run this
            // sample code.
            //
            PutObjectRequest request = new PutObjectRequest
            {
                BucketName  = bucket,
                Key         = "Item",
                ContentBody = "This is sample content..."
            };

            // snippet-end:[s3.dotnet.putobject.async.testputobjectasync.start]

            response = client.PutObject(request);
            Console.WriteLine("Finished PutObject operation for {0}.", request.Key);
            Console.WriteLine("Service Response:");
            Console.WriteLine("-----------------");
            Console.WriteLine("{0}", response);
            Console.Write("\n\n");

            request.Key = "Item1";

            // snippet-start:[s3.dotnet.putobject.async.testputobjectasync.beginputobject]
            asyncResult = client.BeginPutObject(request, null, null);
            while (!asyncResult.IsCompleted)
            {
                //
                // Do some work here
                //
            }
            try
            {
                response = client.EndPutObject(asyncResult);
            }
            catch (AmazonS3Exception s3Exception)
            {
                Console.WriteLine("Caught exception calling EndPutObject:");
                Console.WriteLine(s3Exception);
            }
            // snippet-end:[s3.dotnet.putobject.async.testputobjectasync.beginputobject]

            Console.WriteLine("Finished Async PutObject operation for {0}.", request.Key);
            Console.WriteLine("Service Response:");
            Console.WriteLine("-----------------");
            Console.WriteLine(response);
            Console.Write("\n\n");

            request.Key = "Item2";

            // snippet-start:[s3.dotnet.putobject.async.testputobjectasync.simplecallback]
            asyncResult = client.BeginPutObject(request, SimpleCallback, null);
            // snippet-end:[s3.dotnet.putobject.async.testputobjectasync.simplecallback]

            request.Key = "Item3";

            // snippet-start:[s3.dotnet.putobject.async.testputobjectasync.callbackwithclient]
            asyncResult = client.BeginPutObject(request, CallbackWithClient, client);
            // snippet-end:[s3.dotnet.putobject.async.testputobjectasync.callbackwithclient]

            request.Key = "Item4";

            // snippet-start:[s3.dotnet.putobject.async.testputobjectasync.callbackwithstate]
            asyncResult = client.BeginPutObject(request, CallbackWithState,
                                                new ClientState {
                Client = client, Start = DateTime.Now
            });
            // snippet-end:[s3.dotnet.putobject.async.testputobjectasync.callbackwithstate]

            Thread.Sleep(TimeSpan.FromSeconds(5));
        }