Sample code for 30+ languages & platforms
C

S3 Rename File

See more Amazon S3 (new) Examples

Demonstrates how to rename a file on Amazon S3.

Renaming an object (file) in AWS S3 using the AWS S3 API involves copying the object to a new location with the desired name and then deleting the original object.

Chilkat C Downloads

C
#include <C_CkRest.h>
#include <C_CkAuthAws.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRest rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkAuthAws authAws;
    HCkStringBuilder sbResponse;
    int statusCode;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    rest = CkRest_Create();

    // Connect to the Amazon AWS REST server in the desired region.
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRest_Connect(rest,"s3-us-west-2.amazonaws.com",port,bTls,bAutoReconnect);

    // Provide AWS credentials.
    authAws = CkAuthAws_Create();
    CkAuthAws_putAccessKey(authAws,"AWS_ACCESS_KEY");
    CkAuthAws_putSecretKey(authAws,"AWS_SECRET_KEY");
    CkAuthAws_putServiceName(authAws,"s3");

    // This particular bucket is in the Oregon region, as opposed to the US Standard,
    // therefore the Region must be set appropriately.
    // Also note that we connected to "s3-us-west-2.amazonaws.com".
    CkAuthAws_putRegion(authAws,"us-west-2");

    CkRest_SetAuthAws(rest,authAws);

    // Set the bucket name via the HOST header.
    // In this case, the bucket name is "chilkat.qa".
    // Note that the Host header should use "bucketName.s3.amazonaws.com", not "bucketName.s3-us-west-2.amazonaws.com"
    CkRest_putHost(rest,"chilkat.qa.s3.amazonaws.com");

    // Copy /images/sea_creatures/starfish.jpg to /images/sea_creatures/starfish3.jpg
    // Notice here that the x-amz-copy-source includes the bucket in the source path,
    // but the target path passed to FullRequestNoBody does NOT contain the bucket (because the bucket
    // was already specified in the Host header).
    CkRest_AddHeader(rest,"x-amz-copy-source","/chilkat.qa/images/sea_creatures/starfish.jpg");

    sbResponse = CkStringBuilder_Create();
    success = CkRest_FullRequestNoBodySb(rest,"PUT","/images/sea_creatures/starfish3.jpg",sbResponse);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkAuthAws_Dispose(authAws);
        CkStringBuilder_Dispose(sbResponse);
        return;
    }

    // When successful, the S3 Storage service will respond with a 200 or 204 response code,
    // with an XML body.  
    if (CkRest_getResponseStatusCode(rest) != 200) {
        // Examine the request/response to see what happened.
        printf("response status code = %d\n",CkRest_getResponseStatusCode(rest));
        printf("response status text = %s\n",CkRest_responseStatusText(rest));
        printf("response header: %s\n",CkRest_responseHeader(rest));
        printf("response body: %s\n",CkStringBuilder_getAsString(sbResponse));
        printf("---\n");
        printf("LastRequestStartLine: %s\n",CkRest_lastRequestStartLine(rest));
        printf("LastRequestHeader: %s\n",CkRest_lastRequestHeader(rest));
    }

    printf("%s\n",CkStringBuilder_getAsString(sbResponse));
    printf("Copy Successful.\n");

    // --------------------------------------
    // Now we can delete the original file..
    // --------------------------------------

    // We no longer want to send the x-amz-copy-source header.  Remove it.
    CkRest_RemoveHeader(rest,"x-amz-copy-source");

    success = CkRest_FullRequestNoBodySb(rest,"DELETE","/images/sea_creatures/starfish.jpg",sbResponse);
    if (success == FALSE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkAuthAws_Dispose(authAws);
        CkStringBuilder_Dispose(sbResponse);
        return;
    }

    // If successfully deleted, the response status code is 204 and the response body text will be empty.
    statusCode = CkRest_getResponseStatusCode(rest);
    printf("Response status code = %d\n",statusCode);
    printf("Response text = \n");
    printf("%s\n",CkStringBuilder_getAsString(sbResponse));


    CkRest_Dispose(rest);
    CkAuthAws_Dispose(authAws);
    CkStringBuilder_Dispose(sbResponse);

    }