putBucketInventory.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { checkBucketName } from '../utils/checkBucketName';
  2. import { obj2xml } from '../utils/obj2xml';
  3. type Field = 'Size | LastModifiedDate | ETag | StorageClass | IsMultipartUploaded | EncryptionStatus';
  4. interface Inventory {
  5. id: string;
  6. isEnabled: true | false;
  7. prefix?: string;
  8. OSSBucketDestination: {
  9. format: 'CSV';
  10. accountId: string;
  11. rolename: string;
  12. bucket: string;
  13. prefix?: string;
  14. encryption?:
  15. | { 'SSE-OSS': '' }
  16. | {
  17. 'SSE-KMS': {
  18. keyId: string;
  19. };
  20. };
  21. };
  22. frequency: 'Daily' | 'Weekly';
  23. includedObjectVersions: 'Current' | 'All';
  24. optionalFields?: {
  25. field?: Field[];
  26. };
  27. }
  28. /**
  29. * putBucketInventory
  30. * @param {String} bucketName - bucket name
  31. * @param {Inventory} inventory
  32. * @param {Object} options
  33. */
  34. export async function putBucketInventory(this: any, bucketName: string, inventory: Inventory, options: any = {}) {
  35. const subres: any = Object.assign({ inventory: '', inventoryId: inventory.id }, options.subres);
  36. checkBucketName(bucketName);
  37. const { OSSBucketDestination, optionalFields, includedObjectVersions } = inventory;
  38. const destinationBucketPrefix = 'acs:oss:::';
  39. const rolePrefix = `acs:ram::${OSSBucketDestination.accountId}:role/`;
  40. const paramXMLObj: any = {
  41. InventoryConfiguration: {
  42. Id: inventory.id,
  43. IsEnabled: inventory.isEnabled,
  44. Filter: {
  45. Prefix: inventory.prefix || ''
  46. },
  47. Destination: {
  48. OSSBucketDestination: {
  49. Format: OSSBucketDestination.format,
  50. AccountId: OSSBucketDestination.accountId,
  51. RoleArn: `${rolePrefix}${OSSBucketDestination.rolename}`,
  52. Bucket: `${destinationBucketPrefix}${OSSBucketDestination.bucket}`,
  53. Prefix: OSSBucketDestination.prefix || '',
  54. Encryption: OSSBucketDestination.encryption || ''
  55. }
  56. },
  57. Schedule: {
  58. Frequency: inventory.frequency
  59. },
  60. IncludedObjectVersions: includedObjectVersions,
  61. OptionalFields: {
  62. Field: optionalFields?.field || []
  63. }
  64. }
  65. };
  66. const paramXML = obj2xml(paramXMLObj, {
  67. headers: true,
  68. firstUpperCase: true
  69. });
  70. const params = this._bucketRequestParams('PUT', bucketName, subres, options);
  71. params.successStatuses = [200];
  72. params.mime = 'xml';
  73. params.content = paramXML;
  74. const result = await this.request(params);
  75. return {
  76. status: result.status,
  77. res: result.res
  78. };
  79. }