Mevcut eklenebilir nesnelere içerik eklemek için AppendObject işlemini çağırabilirsiniz.
Kullanım notları
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmaktadır. Aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek için dahili bir uç nokta kullanın. Desteklenen bölgeler ve uç noktalar hakkında ayrıntılı bilgi için Bölgeler ve uç noktalar konusuna bakın.
Bu konuda erişim kimlik bilgileri ortam değişkenlerinden alınmaktadır. Daha fazla bilgi için Erişim kimlik bilgilerini yapılandırma konusuna bakın.
Bu konu, bir OSS uç noktası ile bir OSSClient örneği oluşturmayı göstermektedir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için İstemci yapılandırması konusuna bakın.
İçerik eklemek istediğiniz nesne mevcut değilse AppendObject işlemi eklenebilir bir nesne oluşturur.
-
İçerik eklemek istediğiniz nesne mevcutsa:
Nesne eklenebilir bir nesneyse ve ekleme işleminin başlayacağı belirtilen konum mevcut nesne boyutuna eşitse veriler nesnenin sonuna eklenir.
Nesne eklenebilir bir nesneyse ve ekleme işleminin başlayacağı belirtilen konum mevcut nesne boyutuna eşit değilse PositionNotEqualToLength hatası döndürülür.
Nesne eklenebilir bir nesne değilse ObjectNotAppendable hatası döndürülür.
İzinler
Varsayılan olarak bir Alibaba Cloud hesabı tam izinlere sahiptir. Bir Alibaba Cloud hesabı altındaki RAM kullanıcıları veya RAM rolleri varsayılan olarak herhangi bir izne sahip değildir. Alibaba Cloud hesabı veya hesap yöneticisi, RAM ilkeleri veya Paket İlkesi aracılığıyla işlem izinleri vermelidir.
|
API |
Eylem |
Açıklama |
|
AppendObject |
|
Bir nesneyi mevcut bir nesneye ekleyerek yüklemek için bu işlemi çağırabilirsiniz. |
|
|
Bir nesneyi mevcut bir nesneye ekleyerek yüklerken x-oss-tagging aracılığıyla nesne etiketleri belirtiyorsanız bu izin gereklidir. |
Örnekler
Aşağıdaki örnek kod, ekleme yoluyla yükleme işleminin nasıl gerçekleştirileceğini göstermektedir:
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.AppendObjectRequest;
import com.aliyun.oss.model.AppendObjectResult;
import com.aliyun.oss.model.ObjectMetadata;
import java.io.ByteArrayInputStream;
public class Demo {
public static void main(String[] args) throws Exception {
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your 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 bucket. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
String objectName = "exampledir/exampleobject.txt";
String content1 = "Hello OSS A \n";
String content2 = "Hello OSS B \n";
String content3 = "Hello OSS C \n";
// Specify the region in which 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.
// Call the shutdown method to release associated resources when the OSSClient is no longer in use.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
ObjectMetadata meta = new ObjectMetadata();
// Specify the type of content that you want to upload.
meta.setContentType("text/plain");
// Specify the caching behavior of the web page for the object.
//meta.setCacheControl("no-cache");
// Specify the name of the downloaded object.
//meta.setContentDisposition("attachment;filename=oss_download.txt");
// Specify the content encoding of the object.
//meta.setContentEncoding(OSSConstants.DEFAULT_CHARSET_NAME);
// Specify the request header that is used to check whether the content of the received message is the same as the content of the sent message.
//meta.setContentMD5("ohhnqLBJFiKkPSBO1eNaUA==");
// Specify the expiration time.
//try {
// meta.setExpirationTime(DateUtil.parseRfc822Date("Wed, 08 Jul 2022 16:57:01 GMT"));
//} catch (ParseException e) {
// e.printStackTrace();
//}
// Specify the server-side encryption method. In this example, the method is set to SSE-OSS.
//meta.setServerSideEncryption(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
// Specify the access control list (ACL) of the object. In this example, the ACL of the object is set to private.
//meta.setObjectAcl(CannedAccessControlList.Private);
// Specify the storage class of the object.
//meta.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard);
// You can add parameters whose names are prefixed with x-oss-meta-* when you call the AppendObject operation to create an appendable object. These parameters cannot be included in the request when you append content to an existing appendable object. Parameters whose names are prefixed with x-oss-meta-* are considered as the metadata of the object.
//meta.setHeader("x-oss-meta-author", "Alice");
// Configure multiple parameters by using AppendObjectRequest.
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, objectName, new ByteArrayInputStream(content1.getBytes()),meta);
// Configure a parameter by using AppendObjectRequest.
// Specify the name of the bucket.
//appendObjectRequest.setBucketName(bucketName);
// Specify the object name.
//appendObjectRequest.setKey(objectName);
// Specify the content that you want to append. Two types of content are supported: InputStream and File. In this example, the content is set to InputStream.
//appendObjectRequest.setInputStream(new ByteArrayInputStream(content1.getBytes()));
// Specify the content that you want to append. Two types of content are supported: InputStream and File. In this example, the content is set to File.
//appendObjectRequest.setFile(new File("D:\\localpath\\examplefile.txt"));
// Specify the metadata of the object. You can specify the metadata only when you perform the first append operation on the object.
//appendObjectRequest.setMetadata(meta);
// Perform the first append operation.
// Specify the position from which the append operation starts.
appendObjectRequest.setPosition(0L);
AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
// Calculate the CRC-64 value of the object.
System.out.println(appendObjectResult.getObjectCRC());
// Perform the second append operation.
// NextPosition specifies the position from which the next append operation starts, which is the length of the object.
appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
appendObjectRequest.setInputStream(new ByteArrayInputStream(content2.getBytes()));
appendObjectResult = ossClient.appendObject(appendObjectRequest);
// Perform the third append operation.
appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
appendObjectRequest.setInputStream(new ByteArrayInputStream(content3.getBytes()));
appendObjectResult = ossClient.appendObject(appendObjectRequest);
} 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();
}
}
}
}
İlgili konular
Ekleme yoluyla yükleme için eksiksiz örnek kodu görmek üzere GitHub adresini ziyaret edin.
Ekleme yoluyla yükleme gerçekleştirmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için AppendObject konusuna bakın.