Sample code for 30+ languages & platforms
PureBasic

S3 Add Tags to an Object

See more Amazon S3 (new) Examples

Demonstrates how to add one or more tags to an S3 object.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkAuthAws.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    success.i = 0

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

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Connect to the Amazon AWS REST server in the desired region.
    ; (for us-east-1, we use "s3.amazonaws.com", but for another region, such as us-west-2, we would use "s3-us-west-2.amazonaws.com")
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"s3.amazonaws.com",port,bTls,bAutoReconnect)

    ; Provide AWS credentials.
    authAws.i = CkAuthAws::ckCreate()
    If authAws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
    CkAuthAws::setCkSecretKey(authAws, "AWS_SECRET_KEY")
    CkAuthAws::setCkServiceName(authAws, "s3")
    CkAuthAws::setCkRegion(authAws, "us-east-1")

    CkRest::ckSetAuthAws(rest,authAws)

    ; Set the bucket name via the HOST header.
    ; In this case, the bucket name is "chilkat100".
    ; Note that the Host header should use "bucketName.s3.amazonaws.com", not "bucketName.s3-us-east-1.amazonaws.com"
    ; The same applies to aother regions.  The Host header should simply be <bucketName>.s3.amazonaws.com regardless of the region.
    CkRest::setCkHost(rest, "chilkat100.s3.amazonaws.com")

    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkTag(xml, "Tagging")
    CkXml::ckUpdateChildContent(xml,"TagSet|Tag|Key","plant")
    CkXml::ckUpdateChildContent(xml,"TagSet|Tag|Value","chili pepper")

    sbRequestBody.i = CkStringBuilder::ckCreate()
    If sbRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::ckGetXmlSb(xml,sbRequestBody)

    ; It is important to add the terminating "=" after the "?tagging".
    sbResponse.i = CkStringBuilder::ckCreate()
    If sbResponse.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestSb(rest,"PUT","/chiliPepper.gif?tagging=",sbRequestBody,sbResponse)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        CkXml::ckDispose(xml)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponse)
        ProcedureReturn
    EndIf

    Debug "Response status code: " + Str(CkRest::ckResponseStatusCode(rest))

    ; When successful, the S3 Storage service will respond with a 200 response code,
    ; with an XML body.  
    If CkRest::ckResponseStatusCode(rest) <> 200
        ; Examine the request/response to see what happened.
        Debug "response status code = " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "response status text = " + CkRest::ckResponseStatusText(rest)
        Debug "response header: " + CkRest::ckResponseHeader(rest)
        Debug "response body: " + CkStringBuilder::ckGetAsString(sbResponse)
        Debug "---"
        Debug "LastRequestStartLine: " + CkRest::ckLastRequestStartLine(rest)
        Debug "LastRequestHeader: " + CkRest::ckLastRequestHeader(rest)
    EndIf

    Debug CkStringBuilder::ckGetAsString(sbResponse)
    Debug "Success."


    CkRest::ckDispose(rest)
    CkAuthAws::ckDispose(authAws)
    CkXml::ckDispose(xml)
    CkStringBuilder::ckDispose(sbRequestBody)
    CkStringBuilder::ckDispose(sbResponse)


    ProcedureReturn
EndProcedure