Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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