package storage import ( "context" "fmt" "strings" "time" "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss" ) type ObjectInfo struct { Size int64 ETag string LastModified time.Time } func (c *Client) Stat(ctx context.Context, objectKey string) (ObjectInfo, error) { if strings.TrimSpace(objectKey) == "" { return ObjectInfo{}, fmt.Errorf("对象键不能为空") } result, err := c.client.GetObjectMeta(ctx, &oss.GetObjectMetaRequest{ Bucket: oss.Ptr(c.bucket), Key: oss.Ptr(objectKey), }) if err != nil { return ObjectInfo{}, fmt.Errorf("查询对象元数据: %w", err) } if result == nil { return ObjectInfo{}, fmt.Errorf("对象元数据为空") } if result.ContentLength < 0 { return ObjectInfo{}, fmt.Errorf("对象大小无效: %d", result.ContentLength) } if result.ETag == nil { return ObjectInfo{}, fmt.Errorf("对象 ETag 缺失") } etag := strings.TrimSpace(strings.Trim(strings.TrimSpace(*result.ETag), "\"")) if etag == "" { return ObjectInfo{}, fmt.Errorf("对象 ETag 无效") } if result.LastModified == nil || result.LastModified.IsZero() { return ObjectInfo{}, fmt.Errorf("对象最后修改时间缺失") } return ObjectInfo{ Size: result.ContentLength, ETag: etag, LastModified: *result.LastModified, }, nil } func (c *Client) Delete(ctx context.Context, objectKey string) error { if strings.TrimSpace(objectKey) == "" { return fmt.Errorf("对象键不能为空") } _, err := c.client.DeleteObject(ctx, &oss.DeleteObjectRequest{ Bucket: oss.Ptr(c.bucket), Key: oss.Ptr(objectKey), }) if err != nil { return fmt.Errorf("删除对象: %w", err) } return nil } func (c *Client) PublicURL(objectKey string) string { return strings.TrimRight(c.publicBaseURL, "/") + "/" + strings.TrimLeft(objectKey, "/") } func (c *Client) Bucket() string { return c.bucket }