用工小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.5 KiB

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