사용자가 필터링하는 Cloud Storage 버킷의 객체에 대한 액세스 제어 목록(ACL)을 확인합니다.
코드 샘플
C#
자세한 내용은 Cloud Storage C# API 참조 문서를 확인하세요.
using Google.Apis.Storage.v1.Data;
using Google.Cloud.Storage.V1;
using System;
using System.Collections.Generic;
using System.Linq;
public class PrintFileAclForUserSample
{
public IEnumerable<ObjectAccessControl> PrintFileAclForUser(
string bucketName = "your-unique-bucket-name",
string objectName = "your-object-name",
string userEmail = "user@iam.gserviceaccount.com")
{
var storage = StorageClient.Create();
var storageObject = storage.GetObject(bucketName, objectName, new GetObjectOptions
{
Projection = Projection.Full
});
var fileAclForUser = storageObject.Acl.Where((acl) => acl.Entity == $"user-{userEmail}");
foreach (var acl in fileAclForUser)
{
Console.WriteLine($"{acl.Role}:{acl.Entity}");
}
return fileAclForUser;
}
}
C++
자세한 내용은 Cloud Storage C++ API 참조 문서를 확인하세요.
namespace gcs = google::cloud::storage;
using ::google::cloud::StatusOr;
[](gcs::Client client, std::string const& bucket_name,
std::string const& object_name, std::string const& entity) {
StatusOr<gcs::ObjectAccessControl> acl =
client.GetObjectAcl(bucket_name, object_name, entity);
if (!acl) throw std::runtime_error(acl.status().message());
std::cout << "ACL entry for " << acl->entity() << " in object "
<< acl->object() << " in bucket " << acl->bucket() << " is "
<< *acl << "\n";
}
Go
자세한 내용은 Cloud Storage Go API 참조 문서를 확인하세요.
import (
"context"
"fmt"
"io"
"cloud.google.com/go/storage"
)
// printFileACLForUser lists ACL of the specified object with filter.
func printFileACLForUser(w io.Writer, bucket, object string, entity storage.ACLEntity) error {
// bucket := "bucket-name"
// object := "object-name"
// entity := storage.AllAuthenticatedUsers
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return fmt.Errorf("storage.NewClient: %v", err)
}
defer client.Close()
rules, err := client.Bucket(bucket).ACL().List(ctx)
if err != nil {
return fmt.Errorf("ACLHandle.List: %v", err)
}
for _, r := range rules {
if r.Entity == entity {
fmt.Fprintf(w, "ACL rule role: %v\n", r.Role)
}
}
return nil
}
Node.js
자세한 내용은 Cloud Storage Node.js API 참조 문서를 확인하세요.
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// The ID of your GCS bucket
// const bucketName = 'your-unique-bucket-name';
// The ID of your GCS file
// const fileName = 'your-file-name';
// The email address of the user to check
// const userEmail = 'user-email-to-check';
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client
const storage = new Storage();
async function printFileAclForUser() {
const options = {
// Specify the user
entity: `user-${userEmail}`,
};
// Gets the user's ACL for the file
const [aclObject] = await storage
.bucket(bucketName)
.file(fileName)
.acl.get(options);
console.log(`${aclObject.role}: ${aclObject.entity}`);
}
printFileAclForUser().catch(console.error);
Python
자세한 내용은 Cloud Storage Python API 참조 문서를 확인하세요.
from google.cloud import storage
def print_blob_acl_for_user(bucket_name, blob_name, user_email):
"""Prints out a blob's access control list for a given user."""
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)
# Reload fetches the current ACL from Cloud Storage.
blob.acl.reload()
# You can also use `group`, `domain`, `all_authenticated` and `all` to
# get the roles for different types of entities.
roles = blob.acl.user(user_email).get_roles()
print(roles)
다음 단계
다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참조하세요.