Object Storage Service (OSS) access logging captures detailed information about requests made to your buckets. After you enable logging for a source bucket, OSS will automatically generate and deliver log files hourly to a destination bucket you specify, using a predefined naming convention.
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.