• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

weimin96 / oss-spring-boot-starter / #5

06 Apr 2026 02:14PM UTC coverage: 42.506% (-21.1%) from 63.625%
#5

push

github

web-flow
Merge pull request #9 from weimin96/spring3

Spring3

52 of 509 new or added lines in 8 files covered. (10.22%)

536 of 1261 relevant lines covered (42.51%)

0.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.23
/oss-spring-boot3-starter/src/main/java/com/wiblog/oss/service/TaggingOperations.java
1
package com.wiblog.oss.service;
2

3
import com.wiblog.oss.bean.OssProperties;
4
import com.wiblog.oss.util.Util;
5
import lombok.extern.slf4j.Slf4j;
6
import software.amazon.awssdk.services.s3.S3AsyncClient;
7
import software.amazon.awssdk.services.s3.model.*;
8
import software.amazon.awssdk.transfer.s3.S3TransferManager;
9

10
import java.util.List;
11
import java.util.Map;
12
import java.util.stream.Collectors;
13

14
/**
15
 * 标签操作类(对象级别 & Bucket 级别)。
16
 *
17
 * <p>S3 标签用于成本分配、生命周期规则过滤、跨账号权限策略等场景。
18
 * 每个对象最多支持 10 个标签(键值对),Bucket 最多 50 个。</p>
19
 *
20
 * @author panwm
21
 */
22
@Slf4j
1✔
23
public class TaggingOperations extends Operations {
24

25
    public TaggingOperations(OssProperties ossProperties, S3AsyncClient client,
26
                             S3TransferManager transferManager) {
27
        super(ossProperties, client, transferManager);
1✔
28
    }
1✔
29

30
    // ----------------------------------------------------------------
31
    // 对象标签
32
    // ----------------------------------------------------------------
33

34
    /**
35
     * 获取对象的所有标签。
36
     *
37
     * @param objectName 对象 key
38
     * @return 标签 Map,key 为标签键,value 为标签值
39
     */
40
    public Map<String, String> getObjectTags(String objectName) {
NEW
41
        return getObjectTags(ossProperties.getBucketName(), objectName);
×
42
    }
43

44
    public Map<String, String> getObjectTags(String bucketName, String objectName) {
NEW
45
        GetObjectTaggingRequest req = GetObjectTaggingRequest.builder()
×
NEW
46
                .bucket(bucketName)
×
NEW
47
                .key(Util.formatPath(objectName))
×
NEW
48
                .build();
×
NEW
49
        GetObjectTaggingResponse resp = handleRequest(() -> client.getObjectTagging(req));
×
NEW
50
        if (resp == null) {
×
NEW
51
            return Map.of();
×
52
        }
NEW
53
        return resp.tagSet().stream()
×
NEW
54
                .collect(Collectors.toMap(Tag::key, Tag::value));
×
55
    }
56

57
    /**
58
     * 设置对象标签(覆盖,不是追加)。
59
     *
60
     * @param objectName 对象 key
61
     * @param tags       标签 Map
62
     */
63
    public void setObjectTags(String objectName, Map<String, String> tags) {
NEW
64
        setObjectTags(ossProperties.getBucketName(), objectName, tags);
×
NEW
65
    }
×
66

67
    public void setObjectTags(String bucketName, String objectName, Map<String, String> tags) {
NEW
68
        List<Tag> tagList = tags.entrySet().stream()
×
NEW
69
                .map(e -> Tag.builder().key(e.getKey()).value(e.getValue()).build())
×
NEW
70
                .collect(Collectors.toList());
×
71

NEW
72
        PutObjectTaggingRequest req = PutObjectTaggingRequest.builder()
×
NEW
73
                .bucket(bucketName)
×
NEW
74
                .key(Util.formatPath(objectName))
×
NEW
75
                .tagging(Tagging.builder().tagSet(tagList).build())
×
NEW
76
                .build();
×
NEW
77
        handleRequest(() -> client.putObjectTagging(req));
×
NEW
78
        log.debug("Set {} tags on object [{}]", tags.size(), objectName);
×
NEW
79
    }
×
80

81
    /**
82
     * 追加/更新对象标签(已有标签保留,冲突键覆盖)。
83
     *
84
     * @param objectName 对象 key
85
     * @param tags       要追加/更新的标签
86
     */
87
    public void mergeObjectTags(String objectName, Map<String, String> tags) {
NEW
88
        mergeObjectTags(ossProperties.getBucketName(), objectName, tags);
×
NEW
89
    }
×
90

91
    public void mergeObjectTags(String bucketName, String objectName, Map<String, String> tags) {
NEW
92
        Map<String, String> existing = getObjectTags(bucketName, objectName);
×
93
        // 现有标签 + 新标签合并,新标签覆盖同 key 的旧值
NEW
94
        java.util.HashMap<String, String> merged = new java.util.HashMap<>(existing);
×
NEW
95
        merged.putAll(tags);
×
NEW
96
        setObjectTags(bucketName, objectName, merged);
×
NEW
97
    }
×
98

99
    /**
100
     * 删除对象上的所有标签。
101
     *
102
     * @param objectName 对象 key
103
     */
104
    public void deleteObjectTags(String objectName) {
NEW
105
        deleteObjectTags(ossProperties.getBucketName(), objectName);
×
NEW
106
    }
×
107

108
    public void deleteObjectTags(String bucketName, String objectName) {
NEW
109
        DeleteObjectTaggingRequest req = DeleteObjectTaggingRequest.builder()
×
NEW
110
                .bucket(bucketName)
×
NEW
111
                .key(Util.formatPath(objectName))
×
NEW
112
                .build();
×
NEW
113
        handleRequest(() -> client.deleteObjectTagging(req));
×
NEW
114
        log.debug("Deleted all tags on object [{}]", objectName);
×
NEW
115
    }
×
116

117
    // ----------------------------------------------------------------
118
    // Bucket 标签
119
    // ----------------------------------------------------------------
120

121
    /**
122
     * 获取 Bucket 的所有标签。
123
     */
124
    public Map<String, String> getBucketTags() {
NEW
125
        return getBucketTags(ossProperties.getBucketName());
×
126
    }
127

128
    public Map<String, String> getBucketTags(String bucketName) {
NEW
129
        GetBucketTaggingRequest req = GetBucketTaggingRequest.builder()
×
NEW
130
                .bucket(bucketName)
×
NEW
131
                .build();
×
NEW
132
        GetBucketTaggingResponse resp = handleRequest(() -> client.getBucketTagging(req));
×
NEW
133
        if (resp == null) {
×
NEW
134
            return Map.of();
×
135
        }
NEW
136
        return resp.tagSet().stream()
×
NEW
137
                .collect(Collectors.toMap(Tag::key, Tag::value));
×
138
    }
139

140
    /**
141
     * 设置 Bucket 标签(覆盖)。
142
     */
143
    public void setBucketTags(Map<String, String> tags) {
NEW
144
        setBucketTags(ossProperties.getBucketName(), tags);
×
NEW
145
    }
×
146

147
    public void setBucketTags(String bucketName, Map<String, String> tags) {
NEW
148
        List<Tag> tagList = tags.entrySet().stream()
×
NEW
149
                .map(e -> Tag.builder().key(e.getKey()).value(e.getValue()).build())
×
NEW
150
                .collect(Collectors.toList());
×
151

NEW
152
        PutBucketTaggingRequest req = PutBucketTaggingRequest.builder()
×
NEW
153
                .bucket(bucketName)
×
NEW
154
                .tagging(Tagging.builder().tagSet(tagList).build())
×
NEW
155
                .build();
×
NEW
156
        handleRequest(() -> client.putBucketTagging(req));
×
NEW
157
        log.debug("Set {} tags on bucket [{}]", tags.size(), bucketName);
×
NEW
158
    }
×
159

160
    /**
161
     * 删除 Bucket 的所有标签。
162
     */
163
    public void deleteBucketTags() {
NEW
164
        deleteBucketTags(ossProperties.getBucketName());
×
NEW
165
    }
×
166

167
    public void deleteBucketTags(String bucketName) {
NEW
168
        DeleteBucketTaggingRequest req = DeleteBucketTaggingRequest.builder()
×
NEW
169
                .bucket(bucketName)
×
NEW
170
                .build();
×
NEW
171
        handleRequest(() -> client.deleteBucketTagging(req));
×
NEW
172
        log.debug("Deleted all tags on bucket [{}]", bucketName);
×
NEW
173
    }
×
174
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc