@@ -35,6 +35,8 @@ func main() {
3535 addr := getenv ("LISTEN_ADDR" , ":9000" )
3636
3737 handler := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
38+ log .Printf ("request method=%s host=%s path=%s rawQuery=%s" , r .Method , r .Host , r .URL .Path , r .URL .RawQuery )
39+
3840 if r .URL .Query ().Get ("location" ) != "" {
3941 w .Header ().Set ("Content-Type" , "application/xml" )
4042 fmt .Fprintf (w , "<LocationConstraint>oss-cn-hangzhou</LocationConstraint>" )
@@ -77,21 +79,51 @@ func main() {
7779 http .Error (w , err .Error (), http .StatusInternalServerError )
7880 }
7981 return
80- }
82+ }
83+
84+ if path == objectKey {
85+ w .Header ().Set ("Content-Type" , "application/octet-stream" )
86+ w .Header ().Set ("Content-Length" , fmt .Sprintf ("%d" , len (objectValue )))
87+ if r .Method == http .MethodGet {
88+ _ , _ = w .Write ([]byte (objectValue ))
89+ }
90+ return
91+ }
8192
82- if path == objectKey {
83- w .Header ().Set ("Content-Type" , "application/octet-stream" )
84- w .Header ().Set ("Content-Length" , fmt .Sprintf ("%d" , len (objectValue )))
85- if r .Method == http .MethodGet {
86- _ , _ = w .Write ([]byte (objectValue ))
93+ trimmedObjectDir := strings .TrimSuffix (objectKey , "/" )
94+ objectDir := trimmedObjectDir
95+ if idx := strings .LastIndex (trimmedObjectDir , "/" ); idx >= 0 {
96+ objectDir = trimmedObjectDir [:idx + 1 ]
8797 }
98+ if path == objectDir || path == strings .TrimSuffix (objectDir , "/" ) {
99+ if r .Method == http .MethodHead {
100+ w .WriteHeader (http .StatusOK )
101+ return
102+ }
103+ w .Header ().Set ("Content-Type" , "application/xml" )
104+ if err := xml .NewEncoder (w ).Encode (listBucketResult {
105+ Name : bucketName ,
106+ Prefix : objectDir ,
107+ MaxKeys : 1000 ,
108+ IsTrunc : false ,
109+ Contents : []objectEntry {{
110+ Key : objectKey ,
111+ LastModified : "2026-04-20T00:00:00.000Z" ,
112+ ETag : "\" dummy-etag\" " ,
113+ Type : "Normal" ,
114+ Size : len (objectValue ),
115+ StorageClass : "Standard" ,
116+ }},
117+ }); err != nil {
118+ http .Error (w , err .Error (), http .StatusInternalServerError )
119+ }
120+ return
121+ }
122+
123+ http .NotFound (w , r )
88124 return
89125 }
90126
91- http .NotFound (w , r )
92- return
93- }
94-
95127 http .Error (w , "unsupported method" , http .StatusMethodNotAllowed )
96128 })
97129
0 commit comments