Search code examples
amazon-web-servicesgoamazon-s3aws-sdkaws-sdk-go

AWS Golang SDK fails to copy object when the key contains arabic characters


Using aws-sdk-go, I have been able to sucessfully copy the objects in my s3 bucket when the key contains normal alphanumeric and few special characters like (-,_). But when a key contains an arabic character, the golang aws-sdk throws an error.

NoSuchKey: The specified key does not exist.
    status code: 404, request id: 438DC6xxxxxx, host id: Xp+xxxxxxxxxx

The key in the bucket looks like this:

public/10009/img__١٣٤١١١-1600x1200.jpg

The code is pretty straigh-forward as well:

func copyObject(existingKey, key string, svc *s3.S3) {
    copyObjectInput := &s3.CopyObjectInput{
        Bucket:     aws.String("dummy-bucket"),
        CopySource: aws.String(existingKey),
        Key:        aws.String(key),
    }

    result, err := svc.CopyObject(copyObjectInput)
    if err != nil {
        log.Fatal("Copy failed due to: ", err) // logs the above error here
    }

    spew.Dump(result)
}

I am also printing out the key, just in case: dummy-bucket/public/10009/img__١٣٤١١١-1600x1200.jpg

I was also able to successfully downloaded the image using the aws-sdk-go, with the same key.


Solution

  • According to the documentation, CopySource must be URL-encoded.

    https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#CopyObjectInput

    // The name of the source bucket and key name of the source object, separated
    // by a slash (/). Must be URL-encoded.
    //
    // CopySource is a required field
    CopySource *string `location:"header" locationName:"x-amz-copy-source" type:"string" required:"true"`
    

    Try this,

    import "net/url"
    
    func copyObject(existingKey, key string, svc *s3.S3) {
    
        // existingKey is source bucket and key name separated by "/"
        e := url.QueryEscape(existingKey)
    
        copyObjectInput := &s3.CopyObjectInput{
            Bucket:     aws.String("dummy-bucket"),
            CopySource: aws.String(e),
            Key:        aws.String(key),
        }
    
        result, err := svc.CopyObject(copyObjectInput)
        if err != nil {
            log.Fatal("Copy failed due to: ", err) // logs the above error here
        }
    
        spew.Dump(result)
    }