@@ -77,22 +77,32 @@ public function readObject($urn) {
7777 return $ fh ;
7878 }
7979
80+ private function buildS3Metadata (array $ metadata ): array {
81+ $ result = [];
82+ foreach ($ metadata as $ key => $ value ) {
83+ $ result ['x-amz-meta- ' . $ key ] = $ value ;
84+ }
85+ return $ result ;
86+ }
8087
8188 /**
8289 * Single object put helper
8390 *
8491 * @param string $urn the unified resource name used to identify the object
8592 * @param StreamInterface $stream stream with the data to write
86- * @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0
93+ * @param array $metaData the metadata to set for the object
8794 * @throws \Exception when something goes wrong, message will be logged
8895 */
89- protected function writeSingle (string $ urn , StreamInterface $ stream , ?string $ mimetype = null ): void {
96+ protected function writeSingle (string $ urn , StreamInterface $ stream , array $ metaData ): void {
97+ $ mimetype = $ metaData ['mimetype ' ] ?? null ;
98+ unset($ metaData ['mimetype ' ]);
9099 $ this ->getConnection ()->putObject ([
91100 'Bucket ' => $ this ->bucket ,
92101 'Key ' => $ urn ,
93102 'Body ' => $ stream ,
94103 'ACL ' => 'private ' ,
95104 'ContentType ' => $ mimetype ,
105+ 'Metadata ' => $ this ->buildS3Metadata ($ metaData ),
96106 'StorageClass ' => $ this ->storageClass ,
97107 ] + $ this ->getSSECParameters ());
98108 }
@@ -103,17 +113,20 @@ protected function writeSingle(string $urn, StreamInterface $stream, ?string $mi
103113 *
104114 * @param string $urn the unified resource name used to identify the object
105115 * @param StreamInterface $stream stream with the data to write
106- * @param string|null $mimetype the mimetype to set for the remove object
116+ * @param array $metaData the metadata to set for the object
107117 * @throws \Exception when something goes wrong, message will be logged
108118 */
109- protected function writeMultiPart (string $ urn , StreamInterface $ stream , ?string $ mimetype = null ): void {
119+ protected function writeMultiPart (string $ urn , StreamInterface $ stream , array $ metaData ): void {
120+ $ mimetype = $ metaData ['mimetype ' ] ?? null ;
121+ unset($ metaData ['mimetype ' ]);
110122 $ uploader = new MultipartUploader ($ this ->getConnection (), $ stream , [
111123 'bucket ' => $ this ->bucket ,
112124 'concurrency ' => $ this ->concurrency ,
113125 'key ' => $ urn ,
114126 'part_size ' => $ this ->uploadPartSize ,
115127 'params ' => [
116128 'ContentType ' => $ mimetype ,
129+ 'Metadata ' => $ this ->buildS3Metadata ($ metaData ),
117130 'StorageClass ' => $ this ->storageClass ,
118131 ] + $ this ->getSSECParameters (),
119132 ]);
@@ -131,15 +144,15 @@ protected function writeMultiPart(string $urn, StreamInterface $stream, ?string
131144 }
132145 }
133146
134-
135- /**
136- * @param string $urn the unified resource name used to identify the object
137- * @param resource $stream stream with the data to write
138- * @param string|null $mimetype the mimetype to set for the remove object @since 22.0.0
139- * @throws \Exception when something goes wrong, message will be logged
140- * @since 7.0.0
141- */
142147 public function writeObject ($ urn , $ stream , ?string $ mimetype = null ) {
148+ $ metaData = [];
149+ if ($ mimetype ) {
150+ $ metaData ['mimetype ' ] = $ mimetype ;
151+ }
152+ $ this ->writeObjectWithMetaData ($ urn , $ stream , $ metaData );
153+ }
154+
155+ public function writeObjectWithMetaData (string $ urn , $ stream , array $ metaData ): void {
143156 $ canSeek = fseek ($ stream , 0 , SEEK_CUR ) === 0 ;
144157 $ psrStream = Utils::streamFor ($ stream );
145158
@@ -154,16 +167,16 @@ public function writeObject($urn, $stream, ?string $mimetype = null) {
154167 $ buffer ->seek (0 );
155168 if ($ buffer ->getSize () < $ this ->putSizeLimit ) {
156169 // buffer is fully seekable, so use it directly for the small upload
157- $ this ->writeSingle ($ urn , $ buffer , $ mimetype );
170+ $ this ->writeSingle ($ urn , $ buffer , $ metaData );
158171 } else {
159172 $ loadStream = new Psr7 \AppendStream ([$ buffer , $ psrStream ]);
160- $ this ->writeMultiPart ($ urn , $ loadStream , $ mimetype );
173+ $ this ->writeMultiPart ($ urn , $ loadStream , $ metaData );
161174 }
162175 } else {
163176 if ($ size < $ this ->putSizeLimit ) {
164- $ this ->writeSingle ($ urn , $ psrStream , $ mimetype );
177+ $ this ->writeSingle ($ urn , $ psrStream , $ metaData );
165178 } else {
166- $ this ->writeMultiPart ($ urn , $ psrStream , $ mimetype );
179+ $ this ->writeMultiPart ($ urn , $ psrStream , $ metaData );
167180 }
168181 }
169182 $ psrStream ->close ();
0 commit comments