InstructionFileMetadataStrategy.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Aws\S3\Crypto;
  3. use \Aws\Crypto\MetadataStrategyInterface;
  4. use \Aws\Crypto\MetadataEnvelope;
  5. use \Aws\S3\S3Client;
  6. /**
  7. * Stores and reads encryption MetadataEnvelope information in a file on Amazon
  8. * S3.
  9. *
  10. * A file with the contents of a MetadataEnvelope will be created or read from
  11. * alongside the base file on Amazon S3. The provided client will be used for
  12. * reading or writing this object. A specified suffix (default of '.instruction'
  13. * will be applied to each of the operations involved with the instruction file.
  14. *
  15. * If there is a failure after an instruction file has been uploaded, it will
  16. * not be automatically deleted.
  17. */
  18. class InstructionFileMetadataStrategy implements MetadataStrategyInterface
  19. {
  20. const DEFAULT_FILE_SUFFIX = '.instruction';
  21. private $client;
  22. private $suffix;
  23. /**
  24. * @param S3Client $client Client for use in uploading the instruction file.
  25. * @param string|null $suffix Optional override suffix for instruction file
  26. * object keys.
  27. */
  28. public function __construct(S3Client $client, $suffix = null)
  29. {
  30. $this->suffix = empty($suffix)
  31. ? self::DEFAULT_FILE_SUFFIX
  32. : $suffix;
  33. $this->client = $client;
  34. }
  35. /**
  36. * Places the information in the MetadataEnvelope to a location on S3.
  37. *
  38. * @param MetadataEnvelope $envelope Encryption data to save according to
  39. * the strategy.
  40. * @param array $args Starting arguments for PutObject, used for saving
  41. * extra the instruction file.
  42. *
  43. * @return array Updated arguments for PutObject.
  44. */
  45. public function save(MetadataEnvelope $envelope, array $args)
  46. {
  47. $this->client->putObject([
  48. 'Bucket' => $args['Bucket'],
  49. 'Key' => $args['Key'] . $this->suffix,
  50. 'Body' => json_encode($envelope)
  51. ]);
  52. return $args;
  53. }
  54. /**
  55. * Uses the strategy's client to retrieve the instruction file from S3 and generates
  56. * a MetadataEnvelope from its contents.
  57. *
  58. * @param array $args Arguments from Command and Result that contains
  59. * S3 Object information, relevant headers, and command
  60. * configuration.
  61. *
  62. * @return MetadataEnvelope
  63. */
  64. public function load(array $args)
  65. {
  66. $result = $this->client->getObject([
  67. 'Bucket' => $args['Bucket'],
  68. 'Key' => $args['Key'] . $this->suffix
  69. ]);
  70. $metadataHeaders = json_decode($result['Body'], true);
  71. $envelope = new MetadataEnvelope();
  72. $constantValues = MetadataEnvelope::getConstantValues();
  73. foreach ($constantValues as $constant) {
  74. if (!empty($metadataHeaders[$constant])) {
  75. $envelope[$constant] = $metadataHeaders[$constant];
  76. }
  77. }
  78. return $envelope;
  79. }
  80. }