Bucket Configuration & Access Policies
Initialize a dedicated storage container within the Tencent Cloud Object Storage (COS) interafce. During the provisioning wizard, configure the bucket permissions to Public-Read, Private-Write. This ACL setting enables direct browser rendering of stored assets while restricting external write access. Complete the initialization sequence.
Authentication & Endpoint Resolution
Extract long-term authentication keys via the Cloud Access Management (CAM) portal under the API Keys section. Safeguard the generated SecretId and SecretKey. Record the assigned geographic region (e.g., ap-beijing) and the precise bucket identifier. Assemble the root delivery URL adhering to Tencent's regional routing architecture:
https://[bucket-name]-[appid].cos.[region].myqcloud.com/
Core Storage Service Implementation
Encapsulate the COS Java SDK interactions within a dedicated service layer. The subsequent implementation applies constructor injection, explicit resource lifecycle management, and dynamic MIME resolution to ensure production-grade reliability.
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;
@Service("cosImageProvider")
public class TencentCloudStorageService {
private final String secretKeyId = "AKIDxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private final String secretAccessKey = "AKxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
private final String targetRegion = "ap-beijing";
private final String containerName = "media-assets-prod-987654";
private final String deliveryBaseUrl = "https://media-assets-prod-987654.cos.ap-beijing.myqcloud.com/";
private COSClient establishConnection() {
BasicCOSCredentials auth = new BasicCOSCredentials(secretKeyId, secretAccessKey);
Region geoLocation = new Region(targetRegion);
ClientConfig networkSettings = new ClientConfig(geoLocation);
networkSettings.setHttpProtocol(com.qcloud.cos.http.HttpProtocol.https);
return new COSClient(auth, networkSettings);
}
public String transmitAsset(MultipartFile payload) throws IOException {
String sourceName = payload.getOriginalFilename();
String extension = (sourceName != null && sourceName.contains("."))
? sourceName.substring(sourceName.lastIndexOf(".")) : ".bin";
String uniqueKey = UUID.randomUUID().toString() + extension;
String storageRoute = "public/gallery/" + uniqueKey;
try (COSClient session = establishConnection()) {
ObjectMetadata attributes = new ObjectMetadata();
attributes.setContentLength(payload.getSize());
attributes.setCacheControl("max-age=86400");
attributes.setContentType(payload.getContentType());
PutObjectResult confirmation = session.putObject(
containerName, storageRoute,
payload.getInputStream(), attributes
);
if (confirmation.getETag() != null) {
return deliveryBaseUrl + storageRoute;
}
throw new IllegalStateException("Server did not acknowledge object ingestion");
}
}
}
REST Endpoint Integration
Expose the storage mechanism through a Spring Boot controller optimized for multipart data transmission.
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/v1/assets")
public class AssetGatewayController {
private final TencentCloudStorageService storageHandler;
public AssetGatewayController(TencentCloudStorageService storageHandler) {
this.storageHandler = storageHandler;
}
@PostMapping("/submit")
public ResponseEntity<String> processInboundMedia(@RequestParam("file") MultipartFile dataSource) {
try {
String accessibleLink = storageHandler.transmitAsset(dataSource);
return ResponseEntity.ok(accessibleLink);
} catch (Exception failure) {
return ResponseEntity.internalServerError().body("Transmission aborted: " + failure.getMessage());
}
}
}
Verification Protocol
Validate the integration by executing a POST request to /v1/assets/submit using standard HTTP clients or unit testing frameworks. Populate the file parameter with binary image data encoded as multipart/form-data. A successful operation yields a 200 OK response containing the absolute HTTPS path to the hosted resource. Cross-reference the returned URL against the COS console to verify byte-level accuracy and successful object registration.