Tüm Ürünler
Search
Belge Merkezi

Object Storage Service:Sample code for uploading data streams in parts by using OSS SDK for Java

Son Güncelleme:Oct 10, 2026

Bu konuda, Object Storage Service (OSS) SDK for Java kullanılarak veri akışlarının parçalı yüklenmesine yönelik bir kod örneği sunulmaktadır.

Notlar

  • Bu konuda China (Hangzhou) bölgesi için genel uç nokta kullanılmaktadır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek istiyorsanız dahili bir uç nokta kullanmanızı öneririz. OSS tarafından desteklenen bölgeler ve uç noktalar arasındaki eşlemeler hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.

  • Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınmaktadır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında bilgi için bkz. Erişim kimlik bilgilerini yapılandırma.

  • Bu konuda, genel bir OSS uç noktası kullanılarak bir OSSClient örneği oluşturulmaktadır. Özel bir alan adı veya Security Token Service (STS) kullanarak bir OSSClient örneği oluşturmak istiyorsanız bkz. İstemciyi yapılandırma.

  • InitiateMultipartUpload, UploadPart ve CompleteMultipartUpload işlemlerini içeren tüm parçalı yükleme sürecini tamamlamak için oss:PutObject iznine sahip olmanız gerekir. İzni nasıl vereceğiniz hakkında daha fazla bilgi için bkz. RAM ilkeleri için yaygın örnekler.

Örnek kod

Aşağıdaki örnek kod, veri akışlarını parçalı olarak yükler:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.internal.Mimetypes;
import com.aliyun.oss.model.*;

public class MultipartUploadforStream {
    public static void main(String[] args) throws Exception {
        // In this example, the endpoint for the China (Hangzhou) region is used.
        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 bucket. Example: examplebucket.
        String bucketName = "examplebucket";

        // Specify the full path of the object. Example: exampledir/exampleobject.txt. Do not include the bucket name in the full path.
        String objectName = "exampledir/exampleobject.txt";

        // Specify the path of the local file that you want to upload.
        String filePath = "/Users/localFilePath/example.txt";

        // Specify the region where the bucket is located. For example, if the bucket is located 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 an InitiateMultipartUploadRequest object.
            InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectName);

            ObjectMetadata metadata = new ObjectMetadata();
            if (metadata.getContentType() == null) {
                metadata.setContentType(Mimetypes.getInstance().getMimetype(new File(filePath), objectName));
            }
            System.out.println("Content-Type: " + metadata.getContentType());
            request.setObjectMetadata(metadata);

            // Initiate the multipart upload task and obtain the upload ID in the response.
            InitiateMultipartUploadResult upresult = ossClient.initiateMultipartUpload(request);
            String uploadId = upresult.getUploadId();

            // partETags is a list of PartETags.
            List<PartETag> partETags = new ArrayList<>();

            // Calculate the number of parts.
            File sampleFile = new File(filePath);
            long fileLength = sampleFile.length();
            final long partSize = 5 * 1024 * 1024L; // Set the part size to 5 MB.

            // Upload all parts.
            try (FileInputStream instream = new FileInputStream(sampleFile)) {
                byte[] buffer = new byte[8192]; // Set the buffer.
                long remaining = fileLength;
                int partNumber = 1;

                while (remaining > 0) {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    long bytesToRead = Math.min(partSize, remaining);

                    int bytesRead;
                    long totalRead = 0;
                    while (totalRead < bytesToRead && (bytesRead = instream.read(buffer)) > 0) {
                        baos.write(buffer, 0, bytesRead);
                        totalRead += bytesRead;
                    }

                    // Construct the part stream.
                    InputStream partStream = new ByteArrayInputStream(baos.toByteArray());

                    UploadPartRequest uploadPartRequest = new UploadPartRequest();
                    uploadPartRequest.setBucketName(bucketName);
                    uploadPartRequest.setKey(objectName);
                    uploadPartRequest.setUploadId(uploadId);
                    uploadPartRequest.setInputStream(partStream);
                    uploadPartRequest.setPartSize(totalRead);
                    uploadPartRequest.setPartNumber(partNumber++);

                    UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
                    partETags.add(uploadPartResult.getPartETag());

                    remaining -= totalRead;
                }
            }

            // Create a CompleteMultipartUploadRequest object.
            CompleteMultipartUploadRequest completeMultipartUploadRequest =
                    new CompleteMultipartUploadRequest(bucketName, objectName, uploadId, partETags);

            // Complete the multipart upload task.
            CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
            System.out.println("Upload successful, ETag: " + completeMultipartUploadResult.getETag());
        } 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 a 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 {
            // Shut down the OSSClient.
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}

İlgili Başlıklar

OSS'de parçalı yükleme hakkında daha fazla bilgi için bkz. Parçalı yükleme (Java SDK V1).