Object Storage Service (OSS) generates access logs to record access to resources stored in OSS buckets. After you enable and configure logging for a bucket, OSS generates access logs every hour based on predefined naming rules and then stores the logs in a specific bucket.
Enable log storage
The following code provides an example of how to enable log storage.
const OSS = require('ali-oss')
const client = new OSS({
// Set region to the region where the bucket is located. For example, for China (Hangzhou), set region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before running this code sample, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Set bucket to the name of your bucket.
bucket: 'yourbucketname'
});
async function putBucketLogging () {
try {
const result = await client.putBucketLogging('bucket-name', 'logs/');
console.log(result)
} catch (e) {
console.log(e)
}
}
putBucketLogging();View the log storage configuration
The following code provides an example of how to view the log storage configuration.
const OSS = require('ali-oss')
const client = new OSS({
// Set region to the region where the bucket is located. For example, for China (Hangzhou), set region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before running this code sample, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Set bucket to the name of your bucket.
bucket: 'yourbucketname'
});
async function getBucketLogging() {
try {
const result = await client.getBucketLogging('bucket-name');
console.log(result);
} catch (e) {
console.log(e);
}
}
getBucketLogging();Disable log storage
The following code provides an example of how to disable the log storage feature.
const OSS = require('ali-oss')
const client = new OSS({
// Set region to the region where the bucket is located. For example, for China (Hangzhou), set region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before running this code sample, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Set bucket to the name of your bucket.
bucket: 'yourbucketname'
});
async function deleteBucketLogging () {
try {
const result = await client.deleteBucketLogging('bucket-name');
console.log(result);
} catch (e) {
console.log(e);
}
}
deleteBucketLogging();References
For the complete sample code for log storage, see GitHub examples.
For more information about the API operation to enable log storage, see PutBucketLogging.
For more information about the API operation to view the log storage configuration, see GetBucketLogging.
For more information about the API operation to disable log storage, see DeleteBucketLogging.