Todos los productos
Search
Centro de documentos

Object Storage Service:Copiar objetos (Java SDK V1)

Última actualización: Sep 03, 2026

Copie objetos dentro de un bucket o entre buckets de la misma región.

Notas

  • En este tema se usa el endpoint público de la región China (Hangzhou). Para acceder a OSS desde otros servicios de Alibaba Cloud en la misma región, use un endpoint interno. Para obtener más información sobre las regiones y los endpoints compatibles, consulte Regions and endpoints.

  • En este tema, las credenciales de acceso se obtienen de variables de entorno. Para obtener más información, consulte 配置访问凭证.

  • Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para otras configuraciones, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Client configuration.

  • Debe tener permisos de lectura en el objeto de origen y permisos de lectura y escritura en el bucket de destino.

  • Los buckets de origen y de destino no deben tener directivas de retención configuradas. De lo contrario, la copia fallará y se devolverá el error The object you specified is immutable..

  • No se admite la copia entre regiones. Por ejemplo, no puede copiar un objeto de un bucket en China (Hangzhou) a un bucket en China (Qingdao).

Copiar un objeto pequeño

Use ossClient.copyObject para copiar objetos menores a 1 GB. Este método acepta parámetros de dos formas:

Especificación de parámetros

Descripción

CopyObjectResult copyObject(String sourceBucketName, String sourceKey, String destinationBucketName, String destinationKey)

Especifica los buckets y objetos de origen y destino. Copia el contenido y los metadatos del objeto de origen (copia simple).

CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest)

Especifica metadatos y condiciones de copia para el objeto de destino. Si el origen y el destino son el mismo objeto, reemplaza los metadatos de origen.

Parámetros de CopyObjectRequest:

Parámetro

Descripción

Método

sourceBucketName

Nombre del bucket de origen.

setSourceBucketName(String sourceBucketName)

sourceKey

Nombre del objeto de origen.

setSourceKey(String sourceKey)

destinationBucketName

Nombre del bucket de destino.

setDestinationBucketName(String destinationBucketName)

destinationKey

Nombre del objeto de destino.

setDestinationKey(String destinationKey)

newObjectMetadata

Metadatos del objeto de destino.

setNewObjectMetadata(ObjectMetadata newObjectMetadata)

matchingETagConstraints

Condición de copia. Copia el objeto solo si el ETag de origen coincide con el valor especificado. De lo contrario, devuelve un error.

setMatchingETagConstraints(List<String> matchingETagConstraints)

nonmatchingEtagConstraints

Condición de copia. Copia el objeto solo si el ETag de origen no coincide con el valor especificado. De lo contrario, devuelve un error.

setNonmatchingETagConstraints(List<String> nonmatchingEtagConstraints)

unmodifiedSinceConstraint

Condición de copia. Copia el objeto solo si el origen no se ha modificado desde la hora especificada. De lo contrario, devuelve un error.

setUnmodifiedSinceConstraint(Date unmodifiedSinceConstraint)

modifiedSinceConstraint

Condición de copia. Copia el objeto solo si el origen se modificó después de la hora especificada. De lo contrario, devuelve un error.

setModifiedSinceConstraint(Date modifiedSinceConstraint)

Parámetros de CopyObjectResult:

Parámetro

Descripción

Método

etag

Identificador único del objeto.

String getETag()

lastModified

Hora de la última modificación del objeto.

Date getLastModified()

Copie objetos pequeños mediante uno de los siguientes métodos:

  • Copia simple

    El siguiente ejemplo copia srcexampleobject.txt de srcexamplebucket a desexampleobject.txt en desexamplebucket.

    import com.aliyun.oss.*;
    import com.aliyun.oss.common.auth.*;
    import com.aliyun.oss.common.comm.SignVersion;
    import com.aliyun.oss.model.*;
    
    public class Demo {
        public static void main(String[] args) throws Exception {
            // The China (Hangzhou) region is used as an example. Specify the actual endpoint.
            String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
            // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
            // Specify the name of the source bucket.
            String sourceBucketName = "srcexamplebucket";
            // Specify the full path of the source object. The full path cannot contain the bucket name.
            String sourceKey = "srcexampleobject.txt";
            // Specify the name of the destination bucket. The destination bucket must be in the same region as the source bucket.
            String destinationBucketName = "desexamplebucket";
            // Specify the full path of the destination object. The full path cannot contain the bucket name.
            String destinationKey = "desexampleobject.txt";
            // Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
            String region = "cn-hangzhou";
    
            // Create an OSSClient instance.
            // When the OSSClient instance is no longer used, call the shutdown method to release resources.
            ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
            clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
            OSS ossClient = OSSClientBuilder.create()
            .endpoint(endpoint)
            .credentialsProvider(credentialsProvider)
            .clientConfiguration(clientBuilderConfiguration)
            .region(region)               
            .build();
    
            try {
                // Copy the file.
                CopyObjectResult result = ossClient.copyObject(sourceBucketName, sourceKey, destinationBucketName, destinationKey);
                System.out.println("ETag: " + result.getETag() + " LastModified: " + result.getLastModified());
            } catch (OSSException oe) {
                System.out.println("Caught an OSSException, which means your request made it to OSS, "
                        + "but was rejected with an error response for some reason.");
                System.out.println("Error Message:" + oe.getErrorMessage());
                System.out.println("Error Code:" + oe.getErrorCode());
                System.out.println("Request ID:" + oe.getRequestId());
                System.out.println("Host ID:" + oe.getHostId());
            } catch (ClientException ce) {
                System.out.println("Caught an ClientException, which means the client encountered "
                        + "a serious internal problem while trying to communicate with OSS, "
                        + "such as not being able to access the network.");
                System.out.println("Error Message:" + ce.getMessage());
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }
        }
    }                   
  • Copia mediante CopyObjectRequest

    El siguiente ejemplo usa CopyObjectRequest para copiar srcexampleobject.txt de srcexamplebucket a desexampleobject.txt en desexamplebucket.

    import com.aliyun.oss.*;
    import com.aliyun.oss.common.auth.*;
    import com.aliyun.oss.common.comm.SignVersion;
    import com.aliyun.oss.model.*;
    
    public class Demo {
        public static void main(String[] args) throws Exception {
            // The China (Hangzhou) region is used as an example. Specify the actual endpoint.
            String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
            // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
            EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
            // Specify the name of the source bucket.
            String sourceBucketName = "srcexamplebucket";
            // Specify the full path of the source object. The full path cannot contain the bucket name.
            String sourceKey = "srcexampleobject.txt";
            // Specify the name of the destination bucket. The destination bucket must be in the same region as the source bucket.
            String destinationBucketName = "desexamplebucket";
            // Specify the full path of the destination object. The full path cannot contain the bucket name.
            String destinationKey = "desexampleobject.txt";
            // Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
            String region = "cn-hangzhou";
    
            // Create an OSSClient instance.
            // When the OSSClient instance is no longer used, call the shutdown method to release resources.
            ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
            clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
            OSS ossClient = OSSClientBuilder.create()
            .endpoint(endpoint)
            .credentialsProvider(credentialsProvider)
            .clientConfiguration(clientBuilderConfiguration)
            .region(region)               
            .build();
    
            try {
                // Create a CopyObjectRequest object.
                CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucketName, sourceKey, destinationBucketName, destinationKey);
    
                // Set new file metadata.
                ObjectMetadata meta = new ObjectMetadata();
                meta.setContentType("text/plain");
                // Specify whether to overwrite the destination object if it has the same name. In this example, this parameter is set to true, which indicates that the destination object cannot be overwritten.
                // meta.setHeader("x-oss-forbid-overwrite", "true");
                // Specify the source address for the copy operation.
                // meta.setHeader(OSSHeaders.COPY_OBJECT_SOURCE, "/examplebucket/recode-test.txt");
                // If the ETag of the source object matches the specified ETag, the copy operation is performed and 200 OK is returned.
                // meta.setHeader(OSSHeaders.COPY_OBJECT_SOURCE_IF_MATCH, "5B3C1A2E053D763E1B002CC607C5****");
                // If the ETag of the source object does not match the specified ETag, the copy operation is performed and 200 OK is returned.
                // meta.setHeader(OSSHeaders.COPY_OBJECT_SOURCE_IF_NONE_MATCH, "5B3C1A2E053D763E1B002CC607C5****");
                // If the specified time is the same as or later than the actual modification time of the object, the object is copied and 200 OK is returned.
                // meta.setHeader(OSSHeaders.COPY_OBJECT_SOURCE_IF_UNMODIFIED_SINCE, "2021-12-09T07:01:56.000Z");
                // If the source object has been modified after the specified time, the copy operation is performed.
                // meta.setHeader(OSSHeaders.COPY_OBJECT_SOURCE_IF_MODIFIED_SINCE, "2021-12-09T07:01:56.000Z");
                // Specify how to set the metadata of the destination object. In this example, this parameter is set to COPY, which indicates that the metadata of the source object is copied to the destination object.
                // meta.setHeader(OSSHeaders.COPY_OBJECT_METADATA_DIRECTIVE, "COPY");
                // Specify the server-side encryption algorithm that OSS uses to create the destination object.
                // meta.setHeader(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION, ObjectMetadata.KMS_SERVER_SIDE_ENCRYPTION);
                // The customer master key (CMK) managed by KMS. This parameter is valid only when x-oss-server-side-encryption is set to KMS.
                // meta.setHeader(OSSHeaders.OSS_SERVER_SIDE_ENCRYPTION_KEY_ID, "9468da86-3509-4f8d-a61e-6eab1eac****");
                // Specify the access permissions for the destination object when it is created in OSS. In this example, this parameter is set to Private, which indicates that only the object owner and authorized users have read and write permissions. Other users cannot access the object.
                // meta.setHeader(OSSHeaders.OSS_OBJECT_ACL, CannedAccessControlList.Private);
                // Specify the storage class of the object. In this example, this parameter is set to Standard.
                // meta.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard);
                // Specify the tags for the object. You can specify multiple tags.
                // meta.setHeader(OSSHeaders.OSS_TAGGING, "a:1");
                // Specify how to set the tags for the destination object. In this example, this parameter is set to COPY, which indicates that the tags of the source object are copied to the destination object.
                // meta.setHeader(OSSHeaders.COPY_OBJECT_TAGGING_DIRECTIVE, "COPY");
                copyObjectRequest.setNewObjectMetadata(meta);
    
                // Copy the file.
                CopyObjectResult result = ossClient.copyObject(copyObjectRequest);
                System.out.println("ETag: " + result.getETag() + " LastModified: " + result.getLastModified());
            } catch (OSSException oe) {
                System.out.println("Caught an OSSException, which means your request made it to OSS, "
                        + "but was rejected with an error response for some reason.");
                System.out.println("Error Message:" + oe.getErrorMessage());
                System.out.println("Error Code:" + oe.getErrorCode());
                System.out.println("Request ID:" + oe.getRequestId());
                System.out.println("Host ID:" + oe.getHostId());
            } catch (ClientException ce) {
                System.out.println("Caught an ClientException, which means the client encountered "
                        + "a serious internal problem while trying to communicate with OSS, "
                        + "such as not being able to access the network.");
                System.out.println("Error Message:" + ce.getMessage());
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }
        }
    }                 

Copiar objetos grandes

Para objetos mayores a 1 GB, use la copia multiparte (UploadPartCopy). El proceso consta de tres pasos:

  1. Inicialice una tarea de copia multiparte con ossClient.initiateMultipartUpload.

  2. Copie cada parte con ossClient.uploadPartCopy. Todas las partes, excepto la última, deben ser mayores a 100 KB.

  3. Complete la tarea de copia multiparte con ossClient.completeMultipartUpload.

El siguiente ejemplo usa la copia multiparte para copiar srcexampleobject.txt de srcexamplebucket a desexampleobject.txt en desexamplebucket.

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
import java.util.ArrayList;
import java.util.List;

public class Demo {
    public static void main(String[] args) throws Exception {
        // The endpoint of the China (Hangzhou) region is used as an example. For other regions, specify the actual endpoint.
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Obtain access credentials from environment variables. Before you run this code, ensure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // Specify the name of the source bucket.
        String sourceBucketName = "srcexamplebucket";
        // Specify the full path of the source object. The full path cannot include the bucket name.
        String sourceKey = "srcexampleobject.txt";
        // Specify the name of the destination bucket. The destination bucket must be in the same region as the source bucket.
        String destinationBucketName = "desexamplebucket";
        // Specify the full path of the destination object. The full path cannot include the bucket name.
        String destinationKey = "desexampleobject.txt";
        // Specify the region where the bucket is located. This example uses the China (Hangzhou) region. Set Region to cn-hangzhou.
        String region = "cn-hangzhou";

        // Create an OSSClient instance.
        // When the OSSClient instance is no longer needed, call the shutdown method to release resources.
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);        
        OSS ossClient = OSSClientBuilder.create()
        .endpoint(endpoint)
        .credentialsProvider(credentialsProvider)
        .clientConfiguration(clientBuilderConfiguration)
        .region(region)               
        .build();

        try {
            ObjectMetadata objectMetadata = ossClient.getObjectMetadata(sourceBucketName, sourceKey);
            // Get the size of the file to be copied.
            long contentLength = objectMetadata.getContentLength();

            // Set the part size to 10 MB. The unit is bytes.
            long partSize = 1024 * 1024 * 10;

            // Calculate the total number of parts.
            int partCount = (int) (contentLength / partSize);
            if (contentLength % partSize != 0) {
                partCount++;
            }
            System.out.println("total part count:" + partCount);

            // Initialize the copy task. You can use InitiateMultipartUploadRequest to specify the metadata of the destination object.
            InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(destinationBucketName, destinationKey);
            // Copy the ContentType and UserMetadata of the source file. By default, multipart copy does not copy them.
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType(objectMetadata.getContentType());
            metadata.setUserMetadata(objectMetadata.getUserMetadata());
            initiateMultipartUploadRequest.setObjectMetadata(metadata);
            InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
            String uploadId = initiateMultipartUploadResult.getUploadId();

            // Copy parts.
            List<PartETag> partETags = new ArrayList<PartETag>();
            for (int i = 0; i < partCount; i++) {
                // Calculate the size of each part.
                long skipBytes = partSize * i;
                long size = partSize < contentLength - skipBytes ? partSize : contentLength - skipBytes;

                // Create an UploadPartCopyRequest. You can use UploadPartCopyRequest to specify conditions.
                UploadPartCopyRequest uploadPartCopyRequest =
                        new UploadPartCopyRequest(sourceBucketName, sourceKey, destinationBucketName, destinationKey);
                uploadPartCopyRequest.setUploadId(uploadId);
                uploadPartCopyRequest.setPartSize(size);
                uploadPartCopyRequest.setBeginIndex(skipBytes);
                uploadPartCopyRequest.setPartNumber(i + 1);
                //Map headers = new HashMap();
                // Specify the source address for the copy operation.
                // headers.put(OSSHeaders.COPY_OBJECT_SOURCE, "/examplebucket/desexampleobject.txt");
                // Specify the copy range of the source object. For example, set bytes=0-1023 to copy the first 1024 bytes.
                // headers.put(OSSHeaders.COPY_SOURCE_RANGE, "bytes=0-1023");
                // If the ETag of the source object matches the ETag that you provide, the copy operation is performed and 200 OK is returned.
                // headers.put(OSSHeaders.COPY_OBJECT_SOURCE_IF_MATCH, "5B3C1A2E053D763E1B002CC607C5****");
                // If the ETag of the source object does not match the ETag that you provide, the copy operation is performed and 200 OK is returned.
                // headers.put(OSSHeaders.COPY_OBJECT_SOURCE_IF_NONE_MATCH, "5B3C1A2E053D763E1B002CC607C5****");
                // If the specified time is the same as or later than the actual modification time of the file, the file is copied and 200 OK is returned.
                // headers.put(OSSHeaders.COPY_OBJECT_SOURCE_IF_UNMODIFIED_SINCE, "2021-12-09T07:01:56.000Z");
                // If the source object has been modified after the time that you specified, the copy operation is performed.
                // headers.put(OSSHeaders.COPY_OBJECT_SOURCE_IF_MODIFIED_SINCE, "2021-12-09T07:01:56.000Z");
                // uploadPartCopyRequest.setHeaders(headers);
                UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);

                // Save the returned ETag of the part to partETags.
                partETags.add(uploadPartCopyResult.getPartETag());
            }

            // Complete the multipart copy task.
            CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(
                    destinationBucketName, destinationKey, uploadId, partETags);
            ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

Referencias