Tous les produits
Search
Centre de documentation

Object Storage Service:Tagging d'objets (SDK Android)

Dernière mise à jour :Sep 22, 2026

Object Storage Service (OSS) vous permet de configurer des tags d'objets pour classer vos objets. Cette rubrique explique comment ajouter, interroger et supprimer les tags d'un objet.

Notes d'utilisation

  • Avant d'exécuter l'exemple de code de cette rubrique, créez une instance OSSClient en utilisant un nom de domaine personnalisé ou le Security Token Service (STS). Pour plus d'informations, consultez la section Initialisation (SDK Android).

Configurer le tagging d'objets

Ajouter des tags à un objet lors du chargement

Remarque

Si un objet portant le même nom que celui que vous souhaitez charger existe déjà dans le chemin spécifié, l'objet chargé écrase l'objet existant. Pour éviter cet écrasement, configurez les paramètres appropriés. Pour plus d'informations, consultez la section Empêcher l'écrasement d'objets par des objets portant le même nom.

L'exemple de code suivant montre comment ajouter des tags à un objet lors de son chargement :

// Create an upload request.
// Specify the bucket name, the full path of the object, and the full path of the local file. For example, examplebucket, exampledir/exampleobject.txt, and /storage/emulated/0/oss/examplefile.txt.
// The full path of the object cannot contain the bucket name.
PutObjectRequest put = new PutObjectRequest("examplebucket", "exampledir/exampleobject.txt", "/storage/emulated/0/oss/examplefile.txt");
Map<String, String> tags = new HashMap<String, String>();
// Set the tag information.
tags.put("key1", "value1");
tags.put("Key2", "value2");
String tagHeader = OSSUtils.paramToQueryString(tags, "UTF-8");

ObjectMetadata metadata = new ObjectMetadata();
metadata.setHeader("x-oss-tagging", tagHeader);
put.setMetadata(metadata);

// Set a callback to track the progress of an asynchronous upload.
put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
    @Override
    public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
        Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
    }
});

OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
    @Override
    public void onSuccess(PutObjectRequest request, PutObjectResult result) {
        Log.d("PutObject", "UploadSuccess");
        Log.d("ETag", result.getETag());
        Log.d("RequestId", result.getRequestId());
    }

    @Override
    public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
        // Request exception.
        if (clientExcepion != null) {
            // Client exception, such as a network exception.
            clientExcepion.printStackTrace();
        }
        if (serviceException != null) {
            // Server exception.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

Ajouter ou modifier les tags d'un objet existant

L'exemple de code suivant montre comment ajouter des tags à un objet existant ou modifier ses tags :

// Specify the bucket name. For example, examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the object. For example, exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name.
String objectName = "exampledir/exampleobject.txt";

Map<String, String> tags = new HashMap<String, String>();
// Set the tag information.
tags.put("owner", "John");
tags.put("type", "document");

PutObjectTaggingRequest putObjectTaggingRequest = new PutObjectTaggingRequest(bucketName, objectName, tags);
oss.asyncPutObjectTagging(putObjectTaggingRequest, new OSSCompletedCallback<PutObjectTaggingRequest, PutObjectTaggingResult>() {
    @Override
    public void onSuccess(PutObjectTaggingRequest request, PutObjectTaggingResult result) {
        Log.d("PutTagging", "PutTaggingSuccess");
    }

    @Override
    public void onFailure(PutObjectTaggingRequest request, ClientException clientException, ServiceException serviceException) {
        // Request exception.
        if (clientException != null) {
            // Client exception, such as a network exception.
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Server exception.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

Ajouter des tags à un objet lors de sa copie

L'exemple de code suivant montre comment ajouter des tags à un objet lors de sa copie :

// Create a CopyObject request.
// Specify the source bucket name, the full path of the source object, the destination bucket name, and the full path of the destination object. For example, examplebucket, exampledir/exampleobject.txt, destexamplebucket, and destexampledir/exampleobject.txt.
// The full path of the object cannot contain the bucket name.
CopyObjectRequest copyObjectRequest = new CopyObjectRequest("examplebucket", "exampledir/exampleobject.txt",
        "destexamplebucket", "destexampledir/exampleobject.txt");
ObjectMetadata objectMetadata = new ObjectMetadata();
Map<String, String> tags = new HashMap<String, String>();
// Set tags for the destination object.
tags.put("key1", "value1");
tags.put("key2", "value2");
String tagHeader = OSSUtils.paramToQueryString(tags, "UTF-8");
objectMetadata.setHeader("x-oss-tagging", tagHeader);
// Ignore the tags of the source object and use the tags specified in the request.
objectMetadata.setHeader("x-oss-tagging-directive", "REPLACE");
copyObjectRequest.setNewObjectMetadata(objectMetadata);

OSSAsyncTask copyTask = oss.asyncCopyObject(copyObjectRequest, new OSSCompletedCallback<CopyObjectRequest, CopyObjectResult>() {
    @Override
    public void onSuccess(CopyObjectRequest request, CopyObjectResult result) {
        Log.d("copyObject", "copy success!");
    }

    @Override
    public void onFailure(CopyObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
        // Request exception.
        if (clientExcepion != null) {
            // Client exception, such as a network exception.
            clientExcepion.printStackTrace();
        }
        if (serviceException != null) {
            // Service exception.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

Ajouter des tags à un lien symbolique

L'exemple de code suivant montre comment ajouter des tags à un lien symbolique :

PutSymlinkRequest putSymlink = new PutSymlinkRequest();
// Specify the bucket name. For example, examplebucket.
putSymlink.setBucketName("examplebucket");
// Set the name of the symbolic link.
putSymlink.setObjectKey("yourSymLink");
// Set the name of the object file.
putSymlink.setTargetObjectName("yourTargetObjectName");

Map<String, String> tags = new HashMap<String, String>();
// Set the tag information.
tags.put("key1", "value1");
tags.put("key2", "value2");
String tagHeader = OSSUtils.paramToQueryString(tags, "UTF-8");
ObjectMetadata metadata = new ObjectMetadata();
metadata.setHeader("x-oss-tagging", tagHeader);
putSymlink.setMetadata(metadata);

OSSAsyncTask task = oss.asyncPutSymlink(putSymlink, new OSSCompletedCallback<PutSymlinkRequest,
        PutSymlinkResult>() {
    @Override
    public void onSuccess(PutSymlinkRequest request, PutSymlinkResult result) {
        OSSLog.logInfo("code:"+result.getStatusCode());

    }

    @Override
    public void onFailure(PutSymlinkRequest request, ClientException clientException,
                          ServiceException serviceException) {
        OSSLog.logError("error: "+serviceException.getRawMessage());

    }
});

Interroger les tags d'un objet

L'exemple de code suivant montre comment interroger les tags d'un objet :

// Specify the bucket name. For example, examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the object. For example, exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name.
String objectName = "exampledir/exampleobject.txt";
// Get the tags of the object.
GetObjectTaggingRequest getObjectTaggingRequest = new GetObjectTaggingRequest(bucketName, objectName);
oss.asyncGetObjectTagging(getObjectTaggingRequest, new OSSCompletedCallback<GetObjectTaggingRequest, GetObjectTaggingResult>() {
    @Override
    public void onSuccess(GetObjectTaggingRequest request, GetObjectTaggingResult result) {
        for (Map.Entry<String, String> s : result.getTags().entrySet()) {
            Log.d("tag", "key: " + s.getKey() + ", value: " + s.getValue());
        }
    }

    @Override
    public void onFailure(GetObjectTaggingRequest request, ClientException clientException, ServiceException serviceException) {
        // Request exception.
        if (clientException != null) {
            // Client exception, such as a network exception.
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Server exception.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

Supprimer les tags d'un objet

L'exemple de code suivant montre comment supprimer les tags d'un objet :

// Specify the bucket name. For example, examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the object. For example, exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name.
String objectName = "exampledir/exampleobject.txt";
// Delete the tags of the object.
DeleteObjectTaggingRequest deleteObjectTaggingRequest = new DeleteObjectTaggingRequest(bucketName, objectName);
oss.asyncDeleteObjectTagging(deleteObjectTaggingRequest, new OSSCompletedCallback<DeleteObjectTaggingRequest, DeleteObjectTaggingResult>() {
    @Override
    public void onSuccess(DeleteObjectTaggingRequest request, DeleteObjectTaggingResult result) {
        Log.d("deleteTagging", "deleteTagging success");
    }

    @Override
    public void onFailure(DeleteObjectTaggingRequest request, ClientException clientException, ServiceException serviceException) {
        // Request exception.
        if (clientException != null) {
            // Client exception, such as a network exception.
            clientException.printStackTrace();
        }
        if (serviceException != null) {
            // Server exception.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

Références

  • Pour consulter l'exemple de code complet relatif au tagging d'objets, rendez-vous sur GitHub.

  • Pour plus d'informations sur l'opération API permettant de configurer les tags d'un objet, consultez la section PutObjectTagging.

  • Pour plus d'informations sur l'opération API permettant d'interroger les tags d'un objet, consultez la section GetObjectTagging.

  • Pour plus d'informations sur l'opération API permettant de supprimer les tags d'un objet, consultez la section DeleteObjectTagging.

  • Pour plus d'informations sur la prévention de l'écrasement des objets existants, consultez la section Empêcher l'écrasement d'objets par des objets portant le même nom.

  • Pour plus d'informations sur l'initialisation d'une instance OSSClient, consultez la section Initialisation.