25
25
26
26
namespace tool_objectfs \local \store \s3 ;
27
27
28
+ use coding_exception ;
28
29
use tool_objectfs \local \manager ;
29
30
use tool_objectfs \local \store \object_client_base ;
30
31
use tool_objectfs \local \store \signed_url ;
@@ -62,10 +63,11 @@ class client extends object_client_base {
62
63
*/
63
64
private $ signingmethod ;
64
65
66
+ /**
67
+ * @var mixed
68
+ */
65
69
protected $ bucketkeyprefix ;
66
70
67
- protected $ allowoverride ;
68
-
69
71
/**
70
72
* construct
71
73
* @param mixed $config
@@ -86,7 +88,6 @@ public function __construct($config) {
86
88
$ this ->enablepresignedurls = $ config ->enablepresignedurls ;
87
89
$ this ->signingmethod = $ config ->signingmethod ;
88
90
$ this ->bucketkeyprefix = $ config ->key_prefix ;
89
- $ this ->allowoverride = $ config ->s3_allowoverride ;
90
91
$ this ->set_client ($ config );
91
92
} else {
92
93
parent ::__construct ($ config );
@@ -488,10 +489,6 @@ public function define_client_section($settings, $config) {
488
489
$ settings ->add (new \admin_setting_configtext ('tool_objectfs/key_prefix ' ,
489
490
new \lang_string ('settings:aws:key_prefix ' , 'tool_objectfs ' ),
490
491
new \lang_string ('settings:aws:key_prefix_help ' , 'tool_objectfs ' ), '' ));
491
-
492
- $ settings ->add (new \admin_setting_configcheckbox ('tool_objectfs/s3_overrideobjects ' ,
493
- new \lang_string ('settings:aws:overrideobjects ' , 'tool_objectfs ' ),
494
- new \lang_string ('settings:aws:overrideobjects_help ' , 'tool_objectfs ' ), 0 ));
495
492
496
493
return $ settings ;
497
494
}
@@ -515,15 +512,20 @@ public function upload_to_s3($localpath, $contenthash, array $tags) {
515
512
try {
516
513
$ externalpath = $ this ->get_filepath_from_hash ($ contenthash );
517
514
518
- // TODO upload with tags.
519
515
$ uploader = new \Aws \S3 \ObjectUploader (
520
516
$ this ->client , $ this ->bucket ,
521
517
$ this ->bucketkeyprefix . $ externalpath ,
522
518
$ filehandle ,
523
- 'private ' ,
524
519
);
525
520
$ uploader ->upload ();
526
521
fclose ($ filehandle );
522
+
523
+ // The S3 php api does not support uploading object tags as part of
524
+ // the object uploader, so upload them directly after uploading the object.
525
+ if (!empty ($ tags )) {
526
+ $ this ->set_object_tags ($ contenthash , $ tags );
527
+ }
528
+
527
529
} catch (\Aws \Exception \MultipartUploadException $ e ) {
528
530
$ params = $ e ->getState ()->getId ();
529
531
$ this ->client ->abortMultipartUpload ($ params );
@@ -923,11 +925,14 @@ public function test_set_object_tag(): stdClass {
923
925
return (object ) ['success ' => true , 'details ' => '' ];
924
926
}
925
927
926
- private function convert_array_to_s3_tag_format (array $ tags ): array {
927
-
928
- return [
929
- 'TagSet ' => $ s3tags
930
- ];
928
+ private function convert_tags_to_s3_format (array $ tags ) {
929
+ foreach ($ tags as $ key => $ value ) {
930
+ $ s3tags [] = [
931
+ 'Key ' => $ key ,
932
+ 'Value ' => $ value ,
933
+ ];
934
+ }
935
+ return $ s3tags ;
931
936
}
932
937
933
938
/**
@@ -936,23 +941,14 @@ private function convert_array_to_s3_tag_format(array $tags): array {
936
941
* @param array $tags array of key=>value pairs to set as tags.
937
942
*/
938
943
public function set_object_tags (string $ contenthash , array $ tags ) {
939
- $ key = $ this ->bucketkeyprefix . $ this ->get_filepath_from_hash ($ contenthash );
940
-
941
- // Convert to S3 format.
942
- $ s3tags = [];
943
- foreach ($ tags as $ key => $ value ) {
944
- $ s3tags [] = [
945
- 'Key ' => $ key ,
946
- 'Value ' => $ value ,
947
- ];
948
- }
944
+ $ objectkey = $ this ->bucketkeyprefix . $ this ->get_filepath_from_hash ($ contenthash );
949
945
950
946
// Then put onto object.
951
947
$ this ->client ->putObjectTagging ([
952
948
'Bucket ' => $ this ->bucket ,
953
- 'Key ' => $ key ,
949
+ 'Key ' => $ objectkey ,
954
950
'Tagging ' => [
955
- 'TagSet ' => $ s3tags ,
951
+ 'TagSet ' => $ this -> convert_tags_to_s3_format ( $ tags ) ,
956
952
],
957
953
]);
958
954
}
@@ -966,14 +962,19 @@ public function get_object_tags(string $contenthash): array {
966
962
$ key = $ this ->bucketkeyprefix . $ this ->get_filepath_from_hash ($ contenthash );
967
963
968
964
// Query from S3.
969
- $ tags = $ this ->client ->getObjectTagging ([
965
+ $ result = $ this ->client ->getObjectTagging ([
970
966
'Bucket ' => $ this ->bucket ,
971
967
'Key ' => $ key ,
972
968
]);
973
969
970
+ // Ensure tags are what we expect, and AWS have not changed the format.
971
+ if (empty ($ result ->toArray ()['TagSet ' ])) {
972
+ throw new coding_exception ("Unexpected tag format received " ); // TODO different ex type.
973
+ }
974
+
974
975
// Convert from S3 format to key=>value format.
975
976
$ tagkv = [];
976
- foreach ($ tags [ ' Tagging ' ] ['TagSet ' ] as $ tag ) {
977
+ foreach ($ result -> toArray () ['TagSet ' ] as $ tag ) {
977
978
$ tagkv [$ tag ['Key ' ]] = $ tag ['Value ' ];
978
979
}
979
980
@@ -987,34 +988,4 @@ public function get_object_tags(string $contenthash): array {
987
988
public function supports_object_tagging (): bool {
988
989
return true ;
989
990
}
990
-
991
- // TODO maybe this moves to 'global' objectfs config ?
992
- public function can_override_objects (): bool {
993
- return $ this ->allowoverride ;
994
- }
995
-
996
- public function object_exists (string $ contenthash ): bool {
997
- $ key = $ this ->bucketkeyprefix . $ this ->get_filepath_from_hash ($ contenthash );
998
-
999
- // Query from S3.
1000
- try {
1001
- $ this ->client ->headObject ([
1002
- 'Bucket ' => $ this ->bucket ,
1003
- 'Key ' => $ key ,
1004
- ]);
1005
-
1006
- // Exists, headObject succeeded.
1007
- return true ;
1008
- } catch (\AWS \Exception \AwsException $ e ) {
1009
- $ is404 = false ; // TOOD determine if was 404, i.e. missing object
1010
-
1011
- // Object does not exist.
1012
- if ($ is404 ) {
1013
- return false ;
1014
- }
1015
-
1016
- // Other error - rethrow.
1017
- throw $ e ;
1018
- }
1019
- }
1020
991
}
0 commit comments