Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

s3 head object, erroneously modified object name #2982

Closed
2 of 3 tasks
luoyanpeng-bytedancer opened this issue Jan 23, 2025 · 9 comments
Closed
2 of 3 tasks

s3 head object, erroneously modified object name #2982

luoyanpeng-bytedancer opened this issue Jan 23, 2025 · 9 comments

Comments

@luoyanpeng-bytedancer
Copy link

Acknowledgements

Describe the bug

s3 head object:If the object name starts with"/+ bucket name", the SDK will remove the bucket name from the object name in the path of the URL, causing the request to return 404.

code :
s3@v1.73.2 internal/customizations/update_endpoint.go
func moveBucketNameToHost(u *url.URL, bucket string) { u.Host = bucket + "." + u.Host removeBucketFromPath(u, bucket) }

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

Object names should not be modified

Current Behavior

If the object name starts with"/+ bucket name", the SDK will remove the bucket name from the object name in the path of the URL

Reproduction Steps

Head An object prefixed with "/+ bucket name"

Possible Solution

No response

Additional Information/Context

No response

AWS Go SDK V2 Module Versions Used

github.com/aws/aws-sdk-go-v2 v1.33.0
github.com/aws/aws-sdk-go-v2/config v1.5.0
github.com/aws/aws-sdk-go-v2/service/s3 v1.73.2
github.com/aws/smithy-go v1.22.1

Compiler and Version used

go version go1.21.7 darwin/amd64

Operating System and version

macOS 14.0 (23A344)

@luoyanpeng-bytedancer luoyanpeng-bytedancer added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 23, 2025
@adev-code adev-code self-assigned this Jan 27, 2025
@adev-code adev-code added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-triage This issue or PR still needs to be triaged. labels Jan 27, 2025
@adev-code
Copy link

Hello @luoyanpeng-bytedancer, thanks for reaching out. For doing head object operations on S3 key names that contains characters such as "/" and "+", you would need to encode those characters as they are identified as special characters for S3 (https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html). So for the request, it would need to have the encoded version of the characters. If you have any questions, please do let me know. Thanks.

@adev-code adev-code added p3 This is a minor priority issue response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Jan 29, 2025
@luoyanpeng-bytedancer
Copy link
Author

Hello @luoyanpeng-bytedancer, thanks for reaching out. For doing head object operations on S3 key names that contains characters such as "/" and "+", you would need to encode those characters as they are identified as special characters for S3 (https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html). So for the request, it would need to have the encoded version of the characters. If you have any questions, please do let me know. Thanks.

Why boto3 can not encode "/", and why only "/+ bucket name" has this problem

@adev-code adev-code added needs-review This issue or pull request needs review from a core team member. and removed response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. needs-review This issue or pull request needs review from a core team member. labels Feb 7, 2025
@adev-code
Copy link

The way I have replicated on my side is that:

  1. Uploaded a sample object inside a prefix. From local machine, it looked like this: "/ +bucket-name/sample.png". So, the name of the folder actually has "/" then followed by a space then a "+".
  2. Then uploaded it using the AWS Go SDK to S3. The S3 URI appeared as follows: s3://bucket-name// +bucket-name/sample.png . As shown, the is a double slash added.
  3. Then I ran the Head Object operation and got 200OK. I did not get the 404. Not that I have a / and a space and a + on the name of the object key and it worked fine.

My snippet of code for Head Object:

package main

import (
    "context"
    "fmt"
    "log"
    "os"

    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/s3"
)

const (
    bucketName = "bucket-name"               
    objectKey  = "/ +bucket-name/sample.png" 
    region     = "us-east-1"                                     
)

func main() {
    ctx := context.Background()

    logFile, err := os.OpenFile("s3_head_object_go.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
    if err != nil {
        log.Fatalf("Failed to open log file: %v", err)
    }
    log.SetOutput(logFile) 

    cfg, err := config.LoadDefaultConfig(ctx,
        config.WithRegion(region),
        config.WithClientLogMode(aws.LogRequest|aws.LogResponse),
    )
    if err != nil {
        log.Fatalf("failed to load AWS config: %v", err)
    }

    client := s3.NewFromConfig(cfg)

    resp, err := client.HeadObject(ctx, &s3.HeadObjectInput{
        Bucket: aws.String(bucketName),
        Key:    aws.String(objectKey),
    })
    if err != nil {
        log.Printf("Error occurred: %v\n", err)
        return
    }

    fmt.Println("Metadata:", resp.Metadata)
    fmt.Println("Content Length:", resp.ContentLength, "bytes")
    fmt.Println("Last Modified:", resp.LastModified)
    fmt.Println("ETag:", *resp.ETag)
    fmt.Println("Content Type:", *resp.ContentType)

    log.Printf("HeadObject Response: %+v\n", resp)
}

From the above,
A) How are you uploading the object with the prefix ? Is it straight to the console via drag and drop or via the SDK?
B) When you upload the object on S3, wanted to clarify if the prefix actually have the forward slash in its name?
C) On your code, does the object key match to the S3 URI that is shown on your console?

If you have any other questions, please let me know. Thanks.

@adev-code adev-code added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 14, 2025
@luoyanpeng-bytedancer
Copy link
Author

luoyanpeng-bytedancer commented Feb 15, 2025

objectKey = "/ +bucket-name/sample.png"

There are some misunderstandings here, try objectKey = "/bucket-name/sample.png" and set the endpoint

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 16, 2025
@adev-code
Copy link

I tried with "/bucket-name/sample.png" and I did not get a 404 error. Can you provide (and code) :
1. How you are uploading the object name to S3, is it via SDK, CLI or drag and drop to S3?
2. After uploading, please provide the format of S3 URI shown on S3 Console?
3. The Region of the bucket you are using?
4. When doing the HEAD operation, can you have the logging enabled (https://docs.aws.amazon.com/sdk-for-go/v2/developer-guide/configure-logging.html) : cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithClientLogMode(aws.LogRequest | aws.LogResponse))

@adev-code adev-code added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 20, 2025
Copy link

github-actions bot commented Mar 3, 2025

This issue has not received a response in 1 week. If you want to keep this issue open, please just leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Mar 3, 2025
@luoyanpeng-bytedancer
Copy link
Author

luoyanpeng-bytedancer commented Mar 3, 2025

Sorry, an error message was given, the object name should be "bucket-name/sample.png".
Here is my test code

`region := "ap-southeast-1"
ctx := context.Background()

customResolver := aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
	return aws.Endpoint{
		URL:           "http://s3.ap-southeast-1.amazonaws.com",
		SigningRegion: region,
		Source:        aws.EndpointSourceCustom,
	}, nil
})

cfg, err := config.LoadDefaultConfig(ctx,
	config.WithRegion(region),
	config.WithEndpointResolver(customResolver),
	config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
		Value: aws.Credentials{AccessKeyID: "", SecretAccessKey: ""}}),
)
if err != nil {
	return
}

client := s3.NewFromConfig(cfg)

_, err = client.HeadObject(ctx, &s3.HeadObjectInput{
	Bucket: aws.String("bzy-mv"),
	Key:    aws.String("bzy-mv/object"),
})
if err != nil {
	fmt.Printf("head failed, err = %s", err.Error())
}`

I tried it, if I don't use "AWS. EndpointResolverFunc" is fine, but since I will use storage services from other vendors, I need to use this

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Mar 4, 2025
@lucix-aws lucix-aws removed bug This issue is a bug. p3 This is a minor priority issue labels Mar 4, 2025
@lucix-aws
Copy link
Contributor

aws.EndpointResolver, aws.EndpointResolverFunc, etc. are all deprecated. You will likely experience buggy behavior with these as the S3 endpoint resolution model has evolved.

Please read through https://docs.aws.amazon.com/sdk-for-go/v2/developer-guide/configure-endpoints.html.

since I will use storage services from other vendors

Aside, this SDK is designed to work with official AWS services, I can't guarantee you it will function against anything else.

Since @adev-code has verified that the behavior you're seeing isn't occuring with the (default) v2 endpoint resolution, I'm going to close this.

@lucix-aws lucix-aws closed this as not planned Won't fix, can't repro, duplicate, stale Mar 4, 2025
Copy link

github-actions bot commented Mar 4, 2025

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants