Can we update contents of a specific file in Amazon S3?

*爱你&永不变心* 提交于 2021-02-07 08:33:26

问题


I am using AWS PHP SDK. I uploaded a JSON file to S3 bucket. Now I would like to get the file contents(uploaded to S3 bucket), add some additional text to the grabbed file contents and update that file over the S3 bucket.

What I want is something like this:

  1. file name: userlist.json
  2. grab content of file using S3 provide methods
  3. eg: existing file contents are {'abc','xyz'}
  4. add additional contents {'abc','xyz'}, {'zxv','opiv','cvpo'}
  5. update newly added content into S3 bucket file (userlist.json)

How we can do this ?


回答1:


You can't add data to, or modify just part of an existing s3 object, you need to read the object, make changes to the object, and the write the entire object back to s3.




回答2:


You can overwrite any file in s3. When you write file in same location with same name then it remove old file and replace it with new one.




回答3:


You can follow the following algorithm

step 1 : get the file content from a specific bucket using the function

return $result = $this->s3->getObject(array(
        'Bucket' => bucket,            
        'Key' => 'file_name'
    ));

step 2: Write the return content in a local file.

step 3: Read the file and modify whatever you like. Save the file in local machine. Here you can save the file in different extension.

step 4: Upload the file in the desired bucket. Remember it s3 bucket shall not give immediate update if you overwrite the file with same name.

step 5:End



来源:https://stackoverflow.com/questions/34761076/can-we-update-contents-of-a-specific-file-in-amazon-s3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!