tencent cloud

Cloud Object Storage

Release Notes and Announcements
Release Notes
Announcements
Product Introduction
Overview
Features
Use Cases
Strengths
Concepts
Regions and Access Endpoints
Specifications and Limits
Service Regions and Service Providers
Billing
Billing Overview
Billing Method
Billable Items
Free Tier
Billing Examples
Viewing and Downloading Bill
Payment Overdue
FAQs
Getting Started
Console
Getting Started with COSBrowser
User Guide
Creating Request
Bucket
Object
Data Management
Batch Operation
Global Acceleration
Monitoring and Alarms
Operations Center
Data Processing
Content Moderation
Smart Toolbox
Data Processing Workflow
Application Integration
User Tools
Tool Overview
Installation and Configuration of Environment
COSBrowser
COSCLI (Beta)
COSCMD
COS Migration
FTP Server
Hadoop
COSDistCp
HDFS TO COS
GooseFS-Lite
Online Tools
Diagnostic Tool
Use Cases
Overview
Access Control and Permission Management
Performance Optimization
Accessing COS with AWS S3 SDK
Data Disaster Recovery and Backup
Domain Name Management Practice
Image Processing
Audio/Video Practices
Workflow
Direct Data Upload
Content Moderation
Data Security
Data Verification
Big Data Practice
COS Cost Optimization Solutions
Using COS in the Third-party Applications
Migration Guide
Migrating Local Data to COS
Migrating Data from Third-Party Cloud Storage Service to COS
Migrating Data from URL to COS
Migrating Data Within COS
Migrating Data Between HDFS and COS
Data Lake Storage
Cloud Native Datalake Storage
Metadata Accelerator
GooseFS
Data Processing
Data Processing Overview
Image Processing
Media Processing
Content Moderation
File Processing Service
File Preview
Troubleshooting
Obtaining RequestId
Slow Upload over Public Network
403 Error for COS Access
Resource Access Error
POST Object Common Exceptions
API Documentation
Introduction
Common Request Headers
Common Response Headers
Error Codes
Request Signature
Action List
Service APIs
Bucket APIs
Object APIs
Batch Operation APIs
Data Processing APIs
Job and Workflow
Content Moderation APIs
Cloud Antivirus API
SDK Documentation
SDK Overview
Preparations
Android SDK
C SDK
C++ SDK
.NET(C#) SDK
Flutter SDK
Go SDK
iOS SDK
Java SDK
JavaScript SDK
Node.js SDK
PHP SDK
Python SDK
React Native SDK
Mini Program SDK
Error Codes
Harmony SDK
Endpoint SDK Quality Optimization
Security and Compliance
Data Disaster Recovery
Data Security
Cloud Access Management
FAQs
Popular Questions
General
Billing
Domain Name Compliance Issues
Bucket Configuration
Domain Names and CDN
Object Operations
Logging and Monitoring
Permission Management
Data Processing
Data Security
Pre-signed URL Issues
SDKs
Tools
APIs
Agreements
Service Level Agreement
Privacy Policy
Data Processing And Security Agreement
Contact Us
Glossary
DokumentasiCloud Object StorageSDK DocumentationJava SDKObject OperationsUploading/Downloading Object at Custom Domain Name

Uploading/Downloading Object at Custom Domain Name

PDF
Mode fokus
Ukuran font
Terakhir diperbarui: 2024-02-02 16:24:05

Overview

This document describes how to upload and download objects at a custom domain name. Advanced APIs are used in the samples in this document for uploading and downloading objects. For more information, see Uploading Object and Downloading Object.
Before using a custom domain name to upload and download objects, you first need to set the custom origin domain name for the bucket as instructed in Enabling Custom Origin Domain Name.

Uploading Object at Custom Domain Name

Method prototype

// `UserSpecifiedEndpointBuilder` constructor
com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder(String generalApiEndpoint, String getServiceApiEndpoint);

Sample request

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.UploadResult;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.transfer.TransferManager;
import com.qcloud.cos.transfer.Upload;

public static void uploadFile() {
// 1. Initialize the authentication information (`secretId` and `secretKey`).
COSCredentials cred = new BasicCOSCredentials("secretId", "secretKey");
// 2. Set the bucket region (COS_REGION). For valid values, visit https://www.qcloud.com/document/product/436/6224.
ClientConfig clientConfig = new ClientConfig(new Region("COS_REGION"));
// Set the request protocol. HTTPS is recommended.
clientConfig.setHttpProtocol(HttpProtocol.https);
// If no HTTPS certificate is uploaded when the custom origin domain name is configured, change this line to `clientConfig.setHttpProtocol(HttpProtocol.http);`.
// Set a custom domain name.
UserSpecifiedEndpointBuilder endpointBuilder = new UserSpecifiedEndpointBuilder("generalApiEndpoint", "getServiceApiEndpoint");
clientConfig.setEndpointBuilder(endpointBuilder);
// 3. Generate a COS client.
COSClient cosclient = new COSClient(cred, clientConfig);
// The bucket name must contain `appid`.
String bucketName = "BucketName-APPID";

ExecutorService threadPool = Executors.newFixedThreadPool(32);
// Pass a `threadpool`; otherwise, a single-thread pool will be generated in `TransferManager` by default.
TransferManager transferManager = new TransferManager(cosclient, threadPool);
// Set the object key
String key = "exampleobject";
// Local file path
File localFile = new File("/path/to/localFile");

PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
try {
// Return an async result `Upload`. You can synchronously call `waitForUploadResult` to wait for the upload to complete. If the upload is successful, `UploadResult` will be returned; otherwise, an exception will be reported.
Upload upload = transferManager.upload(putObjectRequest);
UploadResult uploadResult = upload.waitForUploadResult();
System.out.printf("RequestId : %s\\n",uploadResult.getRequestId());
System.out.println(uploadResult.getETag());
System.out.println(uploadResult.getCrc64Ecma());
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

transferManager.shutdownNow();
}
Parameter descriptions
Parameter
Description
Type
generalApiEndpoint
Custom origin domain name
String
getServiceApiEndpoint
The domain name used to get the bucket list. We recommend you enter service.cos.myqcloud.com.
String

Downloading Object at Custom Domain Name

Method prototype

// `UserSpecifiedEndpointBuilder` constructor
com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder(String generalApiEndpoint, String getServiceApiEndpoint);

Sample request

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.endpoint.UserSpecifiedEndpointBuilder;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.transfer.Download;
import com.qcloud.cos.transfer.TransferManager;

public static void downLoadFile() {
// 1. Initialize user authentication information (`secretId`, `secretKey`).
COSCredentials cred = new BasicCOSCredentials("secretId", "secretKey");
// 2. Set the bucket region (COS_REGION). For valid values, visit https://www.qcloud.com/document/product/436/6224.
ClientConfig clientConfig = new ClientConfig(new Region("COS_REGION"));
// Set the request protocol. HTTPS is recommended.
clientConfig.setHttpProtocol(HttpProtocol.https);
// If no HTTPS certificate is uploaded when the custom origin domain name is configured, change this line to `clientConfig.setHttpProtocol(HttpProtocol.http);`.
// Set a custom domain name.
UserSpecifiedEndpointBuilder endpointBuilder = new UserSpecifiedEndpointBuilder("generalApiEndpoint", "getServiceApiEndpoint");
clientConfig.setEndpointBuilder(endpointBuilder);
// 3. Generate a COS client.
COSClient cosclient = new COSClient(cred, clientConfig);
// The bucket name must contain `appid`.
String bucketName = "BucketName-APPID";

ExecutorService threadPool = Executors.newFixedThreadPool(32);
// Pass a `threadpool`; otherwise, a single-thread pool will be generated in `TransferManager` by default.
TransferManager transferManager = new TransferManager(cosclient, threadPool);
// Set the object key
String key = "exampleobject";
// Local file path
File downloadFile = new File("/path/to/localFile");
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
try {
// Return an async result `download`. You can synchronously call `waitForCompletion` to wait for the download to complete. If the download is successful, `void` will be returned; otherwise, an exception will be reported.
Download download = transferManager.download(getObjectRequest, downloadFile);
download.waitForCompletion();
System.out.printf("RequestId : %s\\n",download.getObjectMetadata().getRequestId());
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

transferManager.shutdownNow();
}
Parameter descriptions
Parameter
Description
Type
generalApiEndpoint
Custom origin domain name
String
getServiceApiEndpoint
The domain name used to get the bucket list. We recommend you enter service.cos.myqcloud.com.
String

Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan