热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用php将文件上传到AWSS3-UploadfiletoAWSS3usingphp

IamtryingtocreateaphpscriptwhichwillbeabletouploadatextfiletomyASWS3bucket.我正在

I am trying to create a php script which will be able to upload a text file to my ASW S3 bucket.

我正在尝试创建一个php脚本,它可以将文本文件上传到我的ASW S3存储桶。

I have tried the method which is there on AWS site but sadly that ain't proper, I mean it's not end to end.

我已经尝试过AWS网站上的方法,但遗憾的是这不合适,我的意思是它不是端到端的。

I have installed the AWS PHP SDK on my instance.

我在我的实例上安装了AWS PHP SDK。

Then I did what's written in the sample code i.e.

然后我做了示例代码中写的内容,即

putObject(array(
    'Bucket'       => $bucket,
    'Key'          => $keyname,
    'SourceFile'   => $filepath,
    'ContentType'  => 'text/plain',
    'ACL'          => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY',
    'Metadata'     => array(    
        'param1' => 'value 1',
        'param2' => 'value 2'
    )
));

echo $result['ObjectURL'];

?>

Obviously, I haven't added the aws key nor the aws secret key so it won't work. But then nothing is specified in the tutorial either. So am kinda lost.

显然,我没有添加aws密钥和aws密钥,所以它不起作用。但是,教程中也没有指定任何内容。所以有点失落。

Secondly, I tried using this code :

其次,我尝试使用此代码:

It's also not working.

它也不起作用。

Thirdly, I tried this article.

第三,我试过这篇文章。

It's working when am using it with html but I am not really able to create a php only script where I can just specify the file location, and the file get's uploaded to the server.

当它与html一起使用时它正在工作,但我真的不能创建一个只有php的脚本,我可以只指定文件位置,并将文件get上传到服务器。

Any help is highly appreciated. I searched a lot, but couldn't find anything useful.

任何帮助都非常感谢。我搜索了很多,但找不到任何有用的东西。

1 个解决方案

#1


1  

Just a guess, but did you add your credentials inside your HTML code using hidden inputs? Cause I just had a very quick look at this page: https://aws.amazon.com/articles/1434/ and it seems like you can set your credentials using HTML. And my guess is the class will automatically take care of that.

只是一个猜测,但您是否使用隐藏的输入在HTML代码中添加了凭据?因为我只是快速浏览一下这个页面:https://aws.amazon.com/articles/1434/,好像你可以使用HTML设置你的凭据。我的猜测是班级会自动处理。

If my guess is right, you do need to add the credentials to your instance:

如果我的猜测正确,您需要将凭据添加到您的实例:

// Instantiate the client.
$s3 = S3Client::factory();

like

喜欢

// Instantiate the client.
$s3 = S3Client::factory(array(
  'version' => 'latest',
  'region'  => 'us-west-2', //add correct region
  'credentials' => array(
    'key' => ,
    'secret'  => 
  )
));

It probably depends on the version of the sdk you're using, whether you need above mentioned code or this one (notice the missing credentials array):

它可能取决于您正在使用的sdk的版本,无论您是否需要上述代码或此代码(请注意缺少的凭证数组):

// Instantiate the client.
$s3 = S3Client::factory(array(
  'version' => 'latest',
  'region'  => 'us-west-2', //add correct region
  'key' => ,
  'secret'  => 
));

EDIT: Just to show what exactly worked in my case, this is my complete code. The path I executed: http://myurl.com/index.php?path=./test.txt

编辑:只是为了显示我的案例究竟有用,这是我的完整代码。我执行的路径:http://myurl.com/index.php?path =。/ test.txt

code:

码:

require __DIR__ . '/vendor/autoload.php';

use Aws\S3\S3Client;

$bucket = 'sdl-images';
$keyname = '*** Your Object Key ***';
// $filepath should be absolute path to a file on disk                      
$filepath = $_GET['path'];

// Instantiate the client.
$s3 = S3Client::factory(array(
  'version' => 'latest',
  'region'  => ,
  'credentials' => array(
    'key' => ,
    'secret'  => 
  )
));

// Upload a file.
$result = $s3->putObject(array(
    'Bucket'       => $bucket,
    'Key'          => $keyname,
    'SourceFile'   => $filepath,
    'ContentType'  => 'text/plain',
    'ACL'          => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY'
));

echo $result['ObjectURL'];

推荐阅读
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 单页面应用 VS 多页面应用的区别和适用场景
    本文主要介绍了单页面应用(SPA)和多页面应用(MPA)的区别和适用场景。单页面应用只有一个主页面,所有内容都包含在主页面中,页面切换快但需要做相关的调优;多页面应用有多个独立的页面,每个页面都要加载相关资源,页面切换慢但适用于对SEO要求较高的应用。文章还提到了两者在资源加载、过渡动画、路由模式和数据传递方面的差异。 ... [详细]
  • {moduleinfo:{card_count:[{count_phone:1,count:1}],search_count:[{count_phone:4 ... [详细]
  • {moduleinfo:{card_count:[{count_phone:1,count:1}],search_count:[{count_phone:4 ... [详细]
  • 浅析Mysql数据回滚错误的解决方法_PHP教程:MYSQL的事务处理主要有两种方法。1、用begin,rollback,commit来实现begin开始一个事务rollback事 ... [详细]
  • {moduleinfo:{card_count:[{count_phone:1,count:1}],search_count:[{count_phone:4 ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 使用在线工具jsonschema2pojo根据json生成java对象
    本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。 ... [详细]
  • 本文介绍了解决MySQL数据库Error Code: 1030错误的方法和原因。通过检查文件权限和磁盘空间,注释掉innodb_force_recovery参数等步骤,可以解决无法插入数据和修改文件的问题。 ... [详细]
  • 本文介绍了Oracle存储过程的基本语法和写法示例,同时还介绍了已命名的系统异常的产生原因。 ... [详细]
author-avatar
浐灞半岛商业_318
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有