if (!(pFunc = PyDict_GetItemString(pDict, "Auth"))) {
cout << "Failed to get Function Auth()";
return false;
}
if (!(authR = PyObject_CallFunction(pFunc, "(ss)", authID, authSec))) {
cout << "Auth exec failed!";
return false;
}
if (!(pFunc = PyObject_GetAttrString(pModule, "Bucket"))) {
cout << "Failed to get Function Bucket()";
return false;
}
if (!(bucketR = PyObject_CallFunction(pFunc, "Oss", authR, endpoint, bucket))) {
cout << "Bucket exec failed!" << endl;
return false;
}
char* getobject = "get_object_to_file";
PyObject_CallMethod(bucketR, getobject,"(su)",filename,pathname);
主要代码就是这一部分,执行完返回
Exception oss2.exceptions.RequestError: RequestError() in <module 'threading' from 'C:\Python27\Lib\threading.pyc'> ignored
-------------------------
-------------------------
我写了一段测试代码,没有问题。但是如果我把 endpoint 填错的话,就碰到和你一样的错误。
请你检查下 endpoint 是否正确,自己可以curl一下。
测试代码如下(注意:不是生产代码,错误处理,内存释放基本没有做):
#include <Python.h>
PyObject* GetCallable(PyObject* module, const char* name)
{
PyObject* creator = PyObject_GetAttrString(module, name);
if (!creator) {
printf("%s not found\n", name);
return NULL;
}
if (!PyCallable_Check(creator)) {
printf("%s is not callable\n", name);
Py_DECREF(creator);
return NULL;
}
return creator;
}
int
main(int argc, char *argv[])
{
const char* accessKeyId = "<填写正确的值>";
const char* accessKeySecret = "<填写正确的值>";
const char* endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; /* take hangzhou region as an example */
const char* bucketName = "<填写正确的值>";
Py_Initialize();
PyObject* moduleName = PyString_FromString("oss2");
if (!moduleName) {
printf("bad module name\n");
exit(1);
}
PyObject* module = PyImport_Import(moduleName);
if (!module) {
printf("module not found");
exit(1);
}
PyObject* authCreator = GetCallable(module, "Auth");
if (!authCreator) {
exit(1);
}
PyObject* bucketCreator = GetCallable(module, "Bucket");
if (!bucketCreator) {
exit(1);
}
PyObject* auth = PyObject_CallFunction(authCreator, "ss", accessKeyId, accessKeySecret);
if (!auth) {
printf("failed to instantiate auth\b");
exit(1);
}
PyObject* bucket = PyObject_CallFunction(bucketCreator, "Oss", auth, endpoint, bucketName);
if (!bucket) {
printf("failed to instantiate bucket\n");
exit(1);
}
PyObject_CallMethod(bucket, "get_object_to_file", "ss", "test.txt", "local.txt");
Py_Finalize();
printf("done\n");
return 0;
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。